| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 686 人关注过本帖
标题:我的凯撒密码加密程序—>请高手帮我看看还有哪些要加强的
只看楼主 加入收藏
E_xuan
Rank: 1
等 级:新手上路
帖 子:30
专家分:1
注 册:2009-9-13
结帖率:90%
收藏
 问题点数:0 回复次数:0 
我的凯撒密码加密程序—>请高手帮我看看还有哪些要加强的
本人初学者,望高手赐教。
//主函数
Main.java
package FileDemo;

public class Main{
    public static void main(String[] args){
    new TestAwt();
    }
}

//
package FileDemo;

import java.awt.*;
import java.awt.event.*;

public class TestAwt {
    String str1 = "";
    String str2 = "";
    TextField tf1=null;
    TextField tf2=null;
    int i=0;
    public TestAwt() {
        this.launch();
    }

    public void launch() {

        Frame f = new Frame("Encrypt");
        f.setLayout(null);
        Label l1 = new Label("文件");
        Label l2 = new Label("密钥K");
        tf1 = new TextField(20);
        tf2= new TextField(20);
        Button b1 = new Button("加密");
        Button b2 = new Button("解密");
        Button b3 = new Button("取消");
        Panel p1 = new Panel();
        Panel p2 = new Panel();
        Panel p3 = new Panel();
        f.setLayout(new GridLayout(3, 1));
        f.add(p1);
        f.add(p2);
        f.add(p3);
        p1.setLayout(new BorderLayout());
        p2.setLayout(new BorderLayout());
        p3.setLayout(new FlowLayout());
        p1.add(l1, "West");
        p1.add(tf1, "East");
        p2.add(l2, "West");
        p2.add(tf2, "East");
        p3.add(b1, "East");
        p3.add(b2, "East");
        p3.add(b3, "East");
        f.setVisible(true);
        f.pack();
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        b3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
                ;
            }
        });
    /*    tf1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            //    tf = (TextField) e.getSource();
                //str1 = tf.getText();
            //    new FileInOut(2).fun();
                //tf.setText("");
            }
        });
        tf2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                TextField tf = (TextField) e.getSource();
                str2 = tf.getText();
                tf.setText("");
            }
        }); */
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                str1=tf1.getText();
                str2=tf2.getText();
                i=Integer.parseInt(str2);
                new FileInOut(str1,i).fun();
            }
        });
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new FileEncry(i).fun();
            }
        });
    }
}

//加密程序
package FileDemo;

import *;

public class FileInOut {
    int k;
    String str=null;

    public FileInOut(String str,int k) {
        this.k = k;
        this.str=str;
    }
    public void fun() {
    try{
        File f = new File(str);
        File f1 = new File("D:\\output.txt");
        InputStream in = new FileInputStream(f);
        OutputStream out = new FileOutputStream(f1);
        int ch;
        ch = in.read();
        while (ch != -1) {
            if (ch >= 65 && ch <= 90) {
                {ch = (ch - 65 + k) % 26;
                out.write((char)(ch+65));
                }
            } else if (ch >= 97 && ch <= 122) {
                ch = (ch - 97 + k) % 26;
                out.write((char)(ch+97));
            } else {
                out.write(ch);
            }
            
            ch = in.read();
        }
        in.close();
        out.close();
    }catch(Exception e){
        System.out.println("文件出错!");        
    }
    }
}
//解密程序
package FileDemo;
import *;

public class FileEncry {
    int k;
    String str=null;

    public FileEncry(int k) {
        this.k = k;
    }
    public void fun() {
    try{
        File f = new File("D:\\output.txt");
        File f1 = new File("D:\\decryption.txt");
        InputStream in = new FileInputStream(f);
        OutputStream out = new FileOutputStream(f1);
        int ch;
        ch = in.read();
        while (ch != -1) {
            if (ch >= 65 && ch <= 90) {
                {k=k%26;
                out.write((char)(ch-k));
                }
            } else if (ch >= 97 && ch <= 122) {
                k=k%26;
                out.write((char)(ch-k));
            } else {
                out.write(ch);
            }
            
            ch = in.read();
        }
        in.close();
        out.close();
    }catch(Exception e){
        System.out.println("文件出错!");        
    }
    }
}
搜索更多相关主题的帖子: 密码 凯撒 
2009-10-24 19:22
快速回复:我的凯撒密码加密程序—>请高手帮我看看还有哪些要加强的
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027565 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved