final KeyPanel pp = new
KeyPanel(\密码\
c.add(pp);
JButton jbE = new JButton(\加密\
c.add(jbE);
jbE.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
File file = new File(fp.getFileName());
if (file.exists())encrypt(file.getAbsoluteFile(), pp.getKey());
else
JOptionPane.showMessageDialog(null, \请选择文件!\提示\JOptionPane.OK_OPTION);
});
JButton jbD = new JButton(\解 c.add(jbD);
jbD.addActionListener(new
}
密\
ActionListener() {
public void
actionPerformed(ActionEvent event) {
File file = new
File(fp.getFileName()); ())
decrypt
if (file.exists
(file.getAbsoluteFile(), pp.getKey());
else
10
JOptionPane.showMessageDialog(null, \请选择文件!
\提示\
JOptionPane.OK_OPTION);
/**
* 加密函数 输入: 要加密的文件,密码(由 }
});
}
0-F组成,共48个字符,表示3个8位的密码)如:
*
AD67EA2F3BE6E5ADD368DFE03120B5DF92A8FD8FEC2F0746
其中: AD67EA2F3BE6E5AD
* DES密码一 D368DFE03120B5DF DES密码二
92A8FD8FEC2F0746 DES密码三 输出:
* 对输入的文件加密后,保存到同一文件夹下
增加了\扩展名的文件中。
*/
private void encrypt(File fileIn, String
sKey) { {
byte[] bytK1 = getKeyByStr(sKey.substring(0, 16)); byte[] bytK2 = getKeyByStr(sKey.substring(16, 32));
try {
if (sKey.length() == 48)
11
byte[] bytK3 = getKeyByStr(sKey.substring(32, 48));
FileInputStream fis = new FileInputStream(fileIn); byte[] bytIn = new byte[(int) fileIn.length()]; for (int i = 0; i < fileIn.length(); i++) {
bytIn[i] = (byte) fis.read(); }
// 加密
bytK1),
byte[] bytOut = encryptByDES(encryptByDES(encryptByDES(bytIn, bytK2), bytK3);
String fileOut = fileIn.getPath() + \
FileOutputStream fos = new FileOutputStream(fileOut);
for (int i = 0; i < bytOut.length; i++) {
fos.write((int) bytOut[i]);
}
fos.close();
JOptionPane.showMessageDialog(this, \加密成功!\提示\ JOptionPane.OK_OPTION);
} else
JOptionPane.showMessageDialog(this, \密码长度必须等于48!\错误信息\ JOptionPane.ERROR_MESSAGE);
/**
* 解密函数 输入: 要解密的文件,密码(由 }
} catch (Exception e) { }
e.printStackTrace();
12
0-F组成,共48个字符,表示3个8位的密码)如:
*
AD67EA2F3BE6E5ADD368DFE03120B5DF92A8FD8FEC2F0746
其中: AD67EA2F3BE6E5AD
* DES密码一 D368DFE03120B5DF DES密码二
92A8FD8FEC2F0746 DES密码三 输出:
* 对输入的文件解密后,保存到用户指定的文
件中。
*/
private void decrypt(File fileIn, String
sKey) { {
String strPath =
try {
if (sKey.length() == 48)
fileIn.getPath();
if
(strPath.substring(strPath.length() -
5).toLowerCase()
.equals(\
strPath
= strPath.substring(0, strPath.length() - 5);
else {
13
JOptionPane.showMessageDialog(this, \不是合法的加 密文件!\提示\
JOptionPane.OK_OPTION);
}
JFileChooser
return;
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(\chooser.setSelectedFile(new File(strPath));
// 用户指定要保存
的文件
int ret =
chooser.showSaveDialog(this);
if (ret ==
JFileChooser.APPROVE_OPTION) {
byte[] bytK1 = getKeyByStr(sKey.substring(0, 16)); byte[] bytK2 = getKeyByStr(sKey.substring(16, 32)); byte[] bytK3 = getKeyByStr(sKey.substring(32, 48)); FileInputStream fis = new FileInputStream(fileIn); byte[] bytIn = new byte[(int) fileIn.length()]; for (int i = 0; i < fileIn.length(); i++) { bytIn[i] = (byte) fis.read();
} // 解密
byte[]
bytOut = decryptByDES(decryptByDES(decryptByDES(
14