Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 375|回复: 0

[算法学习]JAVA文件加密器程序

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-11-8 00:05:05 | 显示全部楼层 |阅读模式
    1. import java.awt.*;

    2. import java.awt.event.*;

    3. import javax.swing.*;

    4. import java.io.*;

    5. import java.security.*;

    6. import javax.crypto.*;

    7. import javax.crypto.spec.*;


    8. /**
    9. 文件名:FileEncrypter.java
    10. JDK:1.40以上

    11.    *说明:文件加密
    12. 加密方法:三重DES加密

    13.    *加密过程:
    14. 对选中的文件加密后在同文件夹下生成一个

    15.    *增加了".tdes" 扩展名的加密文件

    16.    *解密过程:对选中的加密文件(必须有".tdes"扩展名)进行解密

    17.    */

    18. public class FileEncrypter extends JFrame{
    19. public static final int WIDTH = 550;
    20. public static final int HEIGHT = 200;
    21. public static void main(String args[]) {
    22.    FileEncrypter fe = new FileEncrypter(); fe.show();
    23. }                       
    复制代码

      
       
       
       

       
      

    1. FileEncrypter(){
    2.   this.setSize(WIDTH,HEIGHT);
    3.   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    4.   this.setResizable(false);
    5.   Toolkit tk = Toolkit.getDefaultToolkit();
    6.   Dimension screenSize = tk.getScreenSize();
    7.   this.setLocation((screenSize.width - WIDTH)/2, (screenSize.height - HEIGHT)/2);
    8.   this.setTitle("文件加密器(TriDES)");
    9.   Container c = this.getContentPane();
    10.   c.setLayout( new FlowLayout());
    11.   final FilePanel fp = new FilePanel("文件选择");
    12.   c.add(fp);
    13.   final KeyPanel pp = new KeyPanel("密码");
    14.   c.add(pp);
    15.   JButton jbE = new JButton("加密");
    16.   c.add(jbE);
    17.   jbE.addActionListener(new ActionListener(){
    18.   public void actionPerformed(ActionEvent event){
    19.   File file = new File(fp.getFileName());
    20.   if (file.exists())
    21.     encrypt(file.getAbsoluteFile(),pp.getKey());
    22.   else
    23.     JOptionPane.showMessageDialog(
    24.   null,"请选择文件!","提示",JOptionPane.OK_OPTION);
    25. }
    26. });
    27.   JButton jbD = new JButton("解密");
    28.   c.add(jbD);
    29.   jbD.addActionListener(new ActionListener(){
    30.     public void actionPerformed(ActionEvent event){
    31.      File file = new File(fp.getFileName());
    32.      if (file.exists())
    33.       decrypt(file.getAbsoluteFile(),pp.getKey());
    34.      else
    35.       JOptionPane.showMessageDialog(null,"请选择文件!","提示",JOptionPane.OK_OPTION);
    36.    }
    37.   });
    38. }
    39. /**
    40. 加密函数
    41. 输入:
    42. 要加密的文件,密码(由0-F组成,共48个字符,表示3个8位的密码)如:
    43. AD67EA2F3BE6E5ADD368DFE03120B5DF92A8FD8FEC2F0746
    44. 其中:
    45. AD67EA2F3BE6E5AD DES密码一
    46. D368DFE03120B5DF DES密码二
    47. 92A8FD8FEC2F0746 DES密码三
    48. 输出:
    49. 对输入的文件加密后,保存到同一文件夹下增加了".tdes"扩展名的文件中。
    50. */
    51. private void encrypt(File fileIn,String sKey){
    52.   try{
    53.    if(sKey.length() == 48){
    54.      byte[] bytK1 = getKeyByStr(sKey.substring(0,16));
    55.      byte[] bytK2 = getKeyByStr(sKey.substring(16,32));
    56.      byte[] bytK3 = getKeyByStr(sKey.substring(32,48));
    57.      FileInputStream fis = new FileInputStream(fileIn);
    58.      byte[] bytIn = new byte[(int)fileIn.length()];
    59.      for(int i = 0;i< fileIn.length();i++){
    60.       bytIn[i] = (byte)fis.read();
    61.      }
    62.     //加密
    63.     byte[] bytOut = encryptByDES(encryptByDES(encryptByDES(bytIn,bytK1),bytK2),bytK3);
    64.     String fileOut = fileIn.getPath() + ".tdes";
    65.     FileOutputStream fos = new FileOutputStream(fileOut);
    66.     for(int i = 0;i< bytOut.length;i++){
    67.       fos.write((int)bytOut[i]);
    68.     }
    69.     fos.close();
    70.     JOptionPane.showMessageDialog(this,"加密成功!","提示",JOptionPane.OK_OPTION);
    71.   }else
    72.     JOptionPane.showMessageDialog(
    73.     this,"密码长度必须等于48!","错误信息",JOptionPane.ERROR_MESSAGE);
    74.   }catch(Exception e){
    75.          e.printStackTrace();
    76.    }
    77. }
    78. /**
    79. 解密函数
    80. 输入:
    81. 要解密的文件,密码(由0-F组成,共48个字符,表示3个8位的密码)如:
    82. AD67EA2F3BE6E5ADD368DFE03120B5DF92A8FD8FEC2F0746
    83. 其中:
    84. AD67EA2F3BE6E5AD DES密码一
    85. D368DFE03120B5DF DES密码二
    86. 92A8FD8FEC2F0746 DES密码三
    87. 输出:
    88. 对输入的文件解密后,保存到用户指定的文件中。
    89. */
    90. private void decrypt(File fileIn,String sKey){
    91.   try{
    92.     if(sKey.length() == 48){
    93.       String strPath = fileIn.getPath();
    94.       if(strPath.substring(strPath.length()-5).toLowerCase().equals(".tdes"))
    95.         strPath = strPath.substring(0,strPath.length()-5);
    96.       else{
    97.         JOptionPane.showMessageDialog(this,"不是合法的加密文件!","提示",JOptionPane.OK_OPTION);
    98.         return;
    99.       }
    100.       JFileChooser chooser = new JFileChooser();
    101.       chooser.setCurrentDirectory(new File("."));
    102.       chooser.setSelectedFile(new File(strPath));
    103.       //用户指定要保存的文件
    104.       int ret = chooser.showSaveDialog(this);
    105.       if(ret==JFileChooser.APPROVE_OPTION){
    106.         byte[] bytK1 = getKeyByStr(sKey.substring(0,16));
    107.         byte[] bytK2 = getKeyByStr(sKey.substring(16,32));
    108.         byte[] bytK3 = getKeyByStr(sKey.substring(32,48));
    109.   
    110.         FileInputStream fis = new FileInputStream(fileIn);
    111.         byte[] bytIn = new byte[(int)fileIn.length()];
    112.         for(int i = 0;i< fileIn.length();i++){
    113.             bytIn[i] = (byte)fis.read();
    114.         }
    115.      //解密
    116.      byte[] bytOut = decryptByDES(decryptByDES(decryptByDES(bytIn,bytK3),bytK2),bytK1);
    117.      File fileOut = chooser.getSelectedFile();
    118.      fileOut.createNewFile();
    119.      FileOutputStream fos = new FileOutputStream(fileOut);
    120.      for(int i = 0;i< bytOut.length;i++){
    121.           fos.write((int)bytOut[i]);
    122.       }
    123.      fos.close();
    124.      JOptionPane.showMessageDialog(this,"解密成功!","提示",JOptionPane.OK_OPTION);
    125.     }
    126.    }else
    127.       JOptionPane.showMessageDialog(this,"密码长度必须等于48!","错误信息",JOptionPane.ERROR_MESSAGE);
    128.   }catch(Exception e){
    129.       JOptionPane.showMessageDialog(this,"解密失败,请核对密码!","提示",JOptionPane.OK_OPTION);
    130.   }
    131. }
    132. /**
    133. 用DES方法加密输入的字节
    134. bytKey需为8字节长,是加密的密码
    135. */
    136. private byte[] encryptByDES(byte[] bytP,byte[] bytKey) throws Exception{
    137.    DESKeySpec desKS = new DESKeySpec(bytKey);
    138.    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    139.    SecretKey sk = skf.generateSecret(desKS);
    140.    Cipher cip = Cipher.getInstance("DES");
    141.    cip.init(Cipher.ENCRYPT_MODE,sk);
    142.    return cip.doFinal(bytP);
    143. }
    144. /**
    145. 用DES方法解密输入的字节
    146. bytKey需为8字节长,是解密的密码
    147. */
    148. private byte[] decryptByDES(byte[] bytE,byte[] bytKey) throws Exception{
    149.    DESKeySpec desKS = new DESKeySpec(bytKey);
    150.    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    151.    SecretKey sk = skf.generateSecret(desKS);
    152.    Cipher cip = Cipher.getInstance("DES");
    153.    cip.init(Cipher.DECRYPT_MODE,sk);
    154.     return cip.doFinal(bytE);
    155. }
    156. /**
    157. 输入密码的字符形式,返回字节数组形式。
    158. 如输入字符串:AD67EA2F3BE6E5AD
    159. 返回字节数组:{173,103,234,47,59,230,229,173}
    160. */
    161. private byte[] getKeyByStr(String str){
    162.    byte[] bRet = new byte[str.length()/2];
    163.    for(int i=0;i< str.length()/2;i++){
    164.      Integer itg =new Integer(16*getChrInt(str.charAt(2*i)) + getChrInt(str.charAt(2*i+1)));
    165.      bRet[i] = itg.byteValue();
    166.    }
    167.     return bRet;
    168. }
    169. /**
    170. 计算一个16进制字符的10进制值
    171. 输入:0-F
    172. */
    173. private int getChrInt(char chr){
    174.   int iRet=0;
    175.   if(chr=="0".charAt(0)) iRet = 0;
    176.   if(chr=="1".charAt(0)) iRet = 1;
    177.   if(chr=="2".charAt(0)) iRet = 2;
    178.   if(chr=="3".charAt(0)) iRet = 3;
    179.   if(chr=="4".charAt(0)) iRet = 4;
    180.   if(chr=="5".charAt(0)) iRet = 5;
    181.   if(chr=="6".charAt(0)) iRet = 6;
    182.   if(chr=="7".charAt(0)) iRet = 7;
    183.   if(chr=="8".charAt(0)) iRet = 8;
    184.   if(chr=="9".charAt(0)) iRet = 9;
    185.   if(chr=="A".charAt(0)) iRet = 10;
    186.   if(chr=="B".charAt(0)) iRet = 11;
    187.   if(chr=="C".charAt(0)) iRet = 12;
    188.   if(chr=="D".charAt(0)) iRet = 13;
    189.   if(chr=="E".charAt(0)) iRet = 14;
    190.   if(chr=="F".charAt(0)) iRet = 15;
    191.     return iRet;
    192.   }
    193. }
    194. /**
    195. 文件选择组件。
    196. */
    197. class FilePanel extends JPanel{
    198.   FilePanel(String str){
    199.    JLabel label = new JLabel(str);
    200.    JTextField fileText = new JTextField(35);
    201.    JButton chooseButton = new JButton("浏览...");
    202.    this.add(label);
    203.    this.add(fileText);
    204.    this.add(chooseButton);
    205.    clickAction ca = new clickAction(this);
    206.    chooseButton.addActionListener(ca);
    207. }
    208. public String getFileName(){
    209. JTextField jtf = (JTextField)this.getComponent(1);
    210. return jtf.getText();
    211. }
    212. private class clickAction implements ActionListener{
    213.   clickAction(Component c){
    214.    cmpt = c;
    215.   }
    216.   public void actionPerformed(ActionEvent event){
    217.     JFileChooser chooser = new JFileChooser();
    218.     chooser.setCurrentDirectory(new File("."));
    219.     int ret = chooser.showOpenDialog(cmpt);
    220.     if(ret==JFileChooser.APPROVE_OPTION){
    221.        JPanel jp = (JPanel)cmpt;
    222.        JTextField jtf = (JTextField)jp.getComponent(1);
    223.        jtf.setText(chooser.getSelectedFile().getPath());
    224.      }
    225.   }
    226.   private Component cmpt;
    227. }
    228. }
    229. /**
    230. 密码生成组件。
    231. */
    232. class KeyPanel extends JPanel{
    233.   KeyPanel(String str){
    234.     JLabel label = new JLabel(str);
    235.     JTextField fileText = new JTextField(35);
    236.     JButton chooseButton = new JButton("随机产生");
    237.     this.add(label);
    238.     this.add(fileText);
    239.     this.add(chooseButton);
    240.     clickAction ca = new clickAction(this);
    241.     chooseButton.addActionListener(ca);
    242. }
    243. //返回生成的密码(48个字符长度)
    244. public String getKey(){
    245.   JTextField jtf = (JTextField)this.getComponent(1);
    246.   return jtf.getText();
    247. }
    248. private class clickAction implements ActionListener{
    249.   clickAction(Component c){
    250.    cmpt = c;
    251.   }
    252. public void actionPerformed(ActionEvent event){
    253. try{
    254.    KeyGenerator kg = KeyGenerator.getInstance("DES");
    255.    kg.init(56);
    256.    Key ke = kg.generateKey();
    257.    byte[] bytK1 = ke.getEncoded();
    258.    ke = kg.generateKey();
    259.    byte[] bytK2 = ke.getEncoded();
    260.    ke = kg.generateKey();
    261.    byte[] bytK3 = ke.getEncoded();
    262.    JPanel jp = (JPanel)cmpt;
    263.    JTextField jtf = (JTextField)jp.getComponent(1);
    264.    jtf.setText(getByteStr(bytK1)+getByteStr(bytK2)+getByteStr(bytK3));
    265. }catch(Exception e){
    266.     e.printStackTrace();
    267.   }
    268. }
    269. private String getByteStr(byte[] byt){
    270.   String strRet = "";
    271.   for(int i=0;i< byt.length;i++){
    272.    //System.out.println(byt[i]);
    273.    strRet += getHexValue((byt[i]&240)/16);
    274.    strRet += getHexValue(byt[i]&15);
    275.   }
    276.   return strRet;
    277. }
    278. private String getHexValue(int s){
    279.    String sRet=null;
    280.    switch (s){
    281.     case 0: sRet = "0";break;
    282.     case 1: sRet = "1";break;
    283.     case 2: sRet = "2";break;
    284.     case 3: sRet = "3";break;
    285.     case 4: sRet = "4";break;
    286.     case 5: sRet = "5";break;
    287.     case 6: sRet = "6";break;
    288.     case 7: sRet = "7";break;
    289.     case 8: sRet = "8";break;
    290.     case 9: sRet = "9";break;
    291.     case 10: sRet = "A";break;
    292.     case 11: sRet = "B";break;
    293.     case 12: sRet = "C";break;
    294.     case 13: sRet = "D";break;
    295.     case 14: sRet = "E";break;
    296.     case 15: sRet = "F";
    297. }
    298.    return sRet;
    299. }
    300.   private Component cmpt;
    301. }
    302. }
    303. 程序运行图:

    304. [img]http://img.javaxxz.com/2014/11/8/000505468.jpg[/img]
    复制代码


      
      
       
       

         
       

         
       
      

    1.                
    复制代码

    源码下载:http://file.javaxxz.com/2014/11/8/000505625.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 10:45 , Processed in 0.303518 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表