通过开发三重DES加密软件,进一步理解对称加密体制的基本原理;了解对称加密算法在文件保护中的应用;了解利用Java开发加密软件的方法和Java中关于加密的类。这里主要介绍定义的两种方法,下面我们来看一下核心代码:
private byte[] encryptByDES(byte[] bytP,byte[] bytKey) throws Exception{
DESKeySpec desKS = new DESKeySpec(bytKey);
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey sk = skf.generateSecret(desKS);
Cipher cip = Cipher.getInstance("DES");
cip.init(Cipher.ENCRYPT_MODE,sk);
return cip.doFinal(bytP);
}
private byte[] decryptByDES(byte[] bytE,byte[] bytKey) throws Exception{
DESKeySpec desKS = new DESKeySpec(bytKey);
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey sk = skf.generateSecret(desKS);
Cipher cip = Cipher.getInstance("DES");
cip.init(Cipher.DECRYPT_MODE,sk);
byte[] te = cip.doFinal(bytE);
return te;
}
下述程序通过调用上述方法来实现加密解密的过程
FileEncrypter()
{ this.setSize(550,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(400, 300);
this.setTitle("文件加密工具(3DES)");
Container c = this.getContentPane();
c.setLayout( new FlowLayout());
JLabel label = new JLabel("文件选择");
c.add(label);
final JTextField fileText = new JTextField(35);
c.add(fileText);
JButton chooseButton = new JButton("浏览...");
chooseButton.addActionListener(new ActionListener() //以下编写"浏览..."按钮的监听和事件
{public void actionPerformed(ActionEvent e)
{JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
int result= chooser.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION)//获得选择的文件绝对路径
{String path = chooser.getSelectedFile().getAbsolutePath();
fileText.setText(path); }
}
}); //"浏览..."按钮的监听和事件编写完毕
c.add(chooseButton);
JLabel label2 = new JLabel("密钥(24个字符):");
c.add(label2);
final JTextField keyText = new JTextField(35);
c.add(keyText);
JButton jbE = new JButton("加密");
c.add(jbE);
jbE.addActionListener(new ActionListener() //以下编写"浏览..."按钮的监听和事件
{ public void actionPerformed(ActionEvent event)
{String wenjian,miyao;
wenjian = fileText.getText();
miyao =keyText.getText();
if("".equals(wenjian) || wenjian==null)
JOptionPane.showMessageDialog (null,"请选择文件!","提示",JOptionPane.OK_OPTION);
else
if("".equals(miyao) || miyao==null)
JOptionPane.showMessageDialog(null,"请输入24字节密钥!","提示",JOptionPane.OK_OPTION);
else{
if(miyao.length()!=24)
{JOptionPane.showMessageDialog(null,"密钥必须为24字节!","提示",JOptionPane.OK_OPTION);}
else{ byte[] key1=miyao.substring(0,8).getBytes();
byte[] key2=miyao.substring(8,16).getBytes();
byte[] key3=miyao.substring(16,24).getBytes();
//将3个密钥分别存入字节型数组中
File file=new File(wenjian);
byte[] plain=bytefromfile(file);
//读取明文并存入字节型数组plain中,bytefromfile()方法定义在后
try{
byte[] bytOut = encryptByDES (encryptByDES(encryptByDES (plain,key1), key2),key3); //实施加密,加密后的密文字节存储在bytOut 中。encryptByDES()方法定义在后
String fileOut = wenjian + ".tdes";
FileOutputStream fos = new FileOutputStream(fileOut);
for(int i = 0;i< bytOut.length;i++){
fos.write((int)bytOut[i]); }
fos.close(); // 将bytOut数组的内容写入新文件
JOptionPane.showMessageDialog(null,"加密成功!","提示",JOptionPane.INFORMATION_MESSAGE);
}catch(Exception e){ JOptionPane.showMessageDialog(null,"加密失败!请检查文件或密钥!","提示",JOptionPane.OK_OPTION); }
}
}
}
}); //编写"加密"按钮的监听和事件完毕
JButton jbD = new JButton("解密");
c.add(jbD);
jbD.addActionListener(new ActionListener() //以下编写"解密"按钮的监听和事件
{ public void actionPerformed(ActionEvent event)
{
String wenjian,wenjian1,miyao;
wenjian = fileText.getText();
miyao =keyText.getText();
if("".equals(wenjian) || wenjian==null)
{JOptionPane.showMessageDialog(null,"请选择文件!","提示",JOptionPane.OK_OPTION); return;}
if(wenjian.substring(wenjian.length()-5).toLowerCase().equals(".tdes"))
if(miyao.length()!=24) {JOptionPane.showMessageDialog(null,"密钥必须为24字节!","提示",JOptionPane.OK_OPTION);return;}
else{
wenjian1 = wenjian.substring(0,wenjian.length()-5);
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setSelectedFile(new File(wenjian1));
//用户指定要保存的文件
int ret = chooser.showSaveDialog(null);
if(ret==0){
byte[] key1=miyao.substring(0,8).getBytes();
byte[] key2=miyao.substring(8,16).getBytes();
byte[] key3=miyao.substring(16,24).getBytes();//读取解密密钥
File file=new File(wenjian);
byte[] miwen=bytefromfile(file);//读取密文
try{
byte[] bytOut = decryptByDES(decryptByDES(decryptByDES (miwen, key3), key2),key1); //实施解密,加密后的密文字节存储在bytOut中。decryptByDES ()方法定义在后
File fileOut = chooser.getSelectedFile();
fileOut.createNewFile();
FileOutputStream fos = new FileOutputStream(fileOut);
for(int i = 0;i< bytOut.length;i++){
fos.write((int)bytOut[i]);
}
fos.close();
JOptionPane.showMessageDialog(null,"解密成功!","提示",JOptionPane. INFORMATION_MESSAGE);
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null,"解密失败!请检查文件或密钥!","提示",JOptionPane.OK_OPTION); }
}
}
else{
JOptionPane.showMessageDialog(null,"不是合法的加密文件!","提示",JOptionPane.OK_OPTION);
return;
}
}
});//编写"解密"按钮的监听和事件完毕
}
private byte[] bytefromfile(File filein){
byte[] TextofFile=new byte[(int)filein.length()];
try{
FileInputStream fin=new FileInputStream(filein);
for(int i=0;i
运行结果如下:
注意使用代码是不要忘记import相关的jar包
下面是对txt文件进行加密和解密
选择需要加密的文件,输入24位密钥,点击加密对其进行加密,生成一个“文件名.txt.tdes”文件
运行后生产txt.tdes文件,其内容为加密乱码
至此加密文件工作完成
将原txt文件删除后,选择txt.tdes文件,输入对应的24位密钥,点击解密,命名后保存,生产txt解密文件内容恢复与原文件相同
小吐槽 :第一次在CSDN上写博客,很多板式都不会用,搞了半天才明白。以后还是要再继续加油熟悉一下。
I’ll continue in my efforts to do anything.