|
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
public class Encry implements ActionListener {
static JFrame w = new JFrame();
public static void main(String args[]) {
JButton btnKey = new JButton("生成密钥");
JButton btnEnc = new JButton("文件加密");
JButton btnDec = new JButton("文件解密");
Encry e = new Encry();
btnKey.addActionListener(e);
btnEnc.addActionListener(e);
btnDec.addActionListener(e);
w.setSize(150, 140);
w.add(btnKey);
w.add(btnEnc);
w.add(btnDec);
w.setLayout(new FlowLayout());
w.setResizable(false);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
String s;
if(arg0.getActionCommand() == "生成密钥") {
try {
File f = new File("C:/key.key");
FileOutputStream fos = new FileOutputStream(f);
for(int i = 0;i < 128; i++)
fos.write((int)(Math.random()*128));
fos.close();
JOptionPane.showMessageDialog(null, "生成密钥成功", "成功", JOptionPane.INFORMATION_MESSAGE);
}catch (Exception ee) {
JOptionPane.showMessageDialog(null, "生成密钥失败", "失败", JOptionPane.ERROR_MESSAGE);
ee.printStackTrace();
}
}
if(arg0.getActionCommand() == "文件加密") {
w.setVisible(false);
s = JOptionPane.showInputDialog(null, "请输入文件路径(注意, 此处使用/代表\\):");
if(JOptionPane.showOptionDialog(null, "确定要加密码?加密后生成的文件将覆盖原来的文件!", "确定?", 2, 2, null, null, null) == 0) {
try {
int key[] = new int[128];
File keyFile = new File("C:/key.key");
File inFile = new File(s);
File outFile = new File(s + "test");
FileInputStream keyFis = new FileInputStream(keyFile);
for(int i = 0;i < 128;i ++)
key[i] = keyFis.read();
keyFis.close();
FileInputStream fis = new FileInputStream(inFile);
FileOutputStream fos = new FileOutputStream(outFile);
int length = fis.available();
for(int i = 0;i < length;i ++)
fos.write(fis.read() + key[i%128]);
fis.close();
fos.close();
FileInputStream fis2 = new FileInputStream(outFile);
FileOutputStream fos2 = new FileOutputStream(inFile);
int length2 = fis2.available();
for(int i = 0;i < length2;i ++)
fos2.write(fis2.read());
fis2.close();
fos2.close();
outFile.delete();
JOptionPane.showMessageDialog(null, "加密文件成功", "成功", JOptionPane.INFORMATION_MESSAGE);
}catch (Exception ee) {
JOptionPane.showMessageDialog(null, "加密文件失败", "失败", JOptionPane.ERROR_MESSAGE);
ee.printStackTrace();
}
w.setVisible(true);
}
}
if(arg0.getActionCommand() == "文件解密") {
w.setVisible(false);
s = JOptionPane.showInputDialog(null, "请输入文件路径(注意, 此处使用/代表\\):");
if(JOptionPane.showOptionDialog(null, "确定要解密码?解密后生成的文件将覆盖原来的文件!", "确定?", 2, 2, null, null, null) == 0) {
try {
int key[] = new int[128];
File keyFile = new File("C:/key.key");
File inFile = new File(s);
File outFile = new File(s + "test");
FileInputStream keyFis = new FileInputStream(keyFile);
for(int i = 0;i < 128;i ++)
key[i] = keyFis.read();
keyFis.close();
FileInputStream fis = new FileInputStream(inFile);
FileOutputStream fos = new FileOutputStream(outFile);
int length = fis.available();
for(int i = 0;i < length;i ++)
fos.write(fis.read() - key[i%128]);
fis.close();
fos.close();
FileInputStream fis2 = new FileInputStream(outFile);
FileOutputStream fos2 = new FileOutputStream(inFile);
int length2 = fis2.available();
for(int i = 0;i < length2;i ++)
fos2.write(fis2.read());
fis2.close();
fos2.close();
outFile.delete();
JOptionPane.showMessageDialog(null, "解密文件成功", "成功", JOptionPane.INFORMATION_MESSAGE);
}catch (Exception ee) {
JOptionPane.showMessageDialog(null, "解密文件失败", "失败", JOptionPane.ERROR_MESSAGE);
ee.printStackTrace();
}
w.setVisible(true);
}
}
}
}
|
|