我的凯撒密码加密程序—>请高手帮我看看还有哪些要加强的
本人初学者,望高手赐教。//主函数
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("文件出错!");
}
}
}