[Java] java文本文件加密解密类 →→→→→进入此内容的聊天室

来自 , 2020-09-27, 写在 Java, 查看 151 次.
URL http://www.code666.cn/view/65658fde
  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. /**
  10.  * 文本文件加密解密类
  11.  *
  12.  * 文件名:FileEncrypter.java JDK:1.40以上 说明:文件加密 加密方法:三重DES加密
  13.  * 加密过程:对选中的文件加密后在同文件夹下生成一个增加了".tdes" 扩展名的加密文件
  14.  *
  15.  * 解密过程:对选中的加密文件(必须有".tdes"扩展名)进行解密
  16.  */
  17. public class Test extends JFrame
  18. {
  19.         public static final int WIDTH = 550;
  20.         public static final int HEIGHT = 200;
  21.  
  22.         public static void main ( String args[] )
  23.         {
  24.                 Test fe = new Test();
  25.                 fe.show();
  26.         }
  27.  
  28.         Test()
  29.         {
  30.                 this.setSize ( WIDTH, HEIGHT );
  31.                 this.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
  32.                 this.setResizable ( false );
  33.                 Toolkit tk = Toolkit.getDefaultToolkit();
  34.                 Dimension screenSize = tk.getScreenSize();
  35.                 this.setLocation ( ( screenSize.width - WIDTH ) / 2,
  36.                                    ( screenSize.height - HEIGHT ) / 2 );
  37.                 this.setTitle ( "文件加密器(TriDES)" );
  38.                 Container c = this.getContentPane();
  39.                 c.setLayout ( new FlowLayout() );
  40.  
  41.                 final FilePanel fp = new FilePanel ( "文件选择" );
  42.                 c.add ( fp );
  43.  
  44.                 final KeyPanel pp = new KeyPanel ( "密码" );
  45.                 c.add ( pp );
  46.  
  47.                 JButton jbE = new JButton ( "加密" );
  48.                 c.add ( jbE );
  49.                 jbE.addActionListener ( new ActionListener()
  50.                 {
  51.                         public void actionPerformed ( ActionEvent event )
  52.                         {
  53.                                 File file = new File ( fp.getFileName() );
  54.                                 if ( file.exists() )
  55.                                         encrypt ( file.getAbsoluteFile(), pp.getKey() );
  56.                                 else
  57.                                         JOptionPane.showMessageDialog ( null, "请选择文件!", "提示",
  58.                                                                         JOptionPane.OK_OPTION );
  59.                         }
  60.                 } );
  61.                 JButton jbD = new JButton ( "解密" );
  62.                 c.add ( jbD );
  63.                 jbD.addActionListener ( new ActionListener()
  64.                 {
  65.                         public void actionPerformed ( ActionEvent event )
  66.                         {
  67.                                 File file = new File ( fp.getFileName() );
  68.                                 if ( file.exists() )
  69.                                         decrypt ( file.getAbsoluteFile(), pp.getKey() );
  70.                                 else
  71.                                         JOptionPane.showMessageDialog ( null, "请选择文件!", "提示",
  72.                                                                         JOptionPane.OK_OPTION );
  73.                         }
  74.                 } );
  75.         }
  76.  
  77.         /**
  78.          * 加密函数 输入: 要加密的文件,密码(由0-F组成,共48个字符,表示3个8位的密码)如:
  79.          * AD67EA2F3BE6E5ADD368DFE03120B5DF92A8FD8FEC2F0746 其中: AD67EA2F3BE6E5AD
  80.          * DES密码一 D368DFE03120B5DF DES密码二 92A8FD8FEC2F0746 DES密码三 输出:
  81.          * 对输入的文件加密后,保存到同一文件夹下增加了".tdes"扩展名的文件中。
  82.          */
  83.         private void encrypt ( File fileIn, String sKey )
  84.         {
  85.                 try
  86.                 {
  87.                         if ( sKey.length() == 48 )
  88.                         {
  89.                                 byte[] bytK1 = getKeyByStr ( sKey.substring ( 0, 16 ) );
  90.                                 byte[] bytK2 = getKeyByStr ( sKey.substring ( 16, 32 ) );
  91.                                 byte[] bytK3 = getKeyByStr ( sKey.substring ( 32, 48 ) );
  92.  
  93.                                 FileInputStream fis = new FileInputStream ( fileIn );
  94.                                 byte[] bytIn = new byte[ ( int ) fileIn.length() ];
  95.                                 for ( int i = 0; i < fileIn.length(); i++ )
  96.                                 {
  97.                                         bytIn[i] = ( byte ) fis.read();
  98.                                 }
  99.                                 // 加密
  100.                                 byte[] bytOut = encryptByDES (
  101.                                                     encryptByDES ( encryptByDES ( bytIn, bytK1 ), bytK2 ), bytK3 );
  102.                                 String fileOut = fileIn.getPath() + ".tdes";
  103.                                 FileOutputStream fos = new FileOutputStream ( fileOut );
  104.                                 for ( int i = 0; i < bytOut.length; i++ )
  105.                                 {
  106.                                         fos.write ( ( int ) bytOut[i] );
  107.                                 }
  108.                                 fos.close();
  109.                                 JOptionPane.showMessageDialog ( this, "加密成功!", "提示",
  110.                                                                 JOptionPane.OK_OPTION );
  111.                         }
  112.                         else
  113.                                 JOptionPane.showMessageDialog ( this, "密码长度必须等于48!", "错误信息",
  114.                                                                 JOptionPane.ERROR_MESSAGE );
  115.                 }
  116.                 catch ( Exception e )
  117.                 {
  118.                         e.printStackTrace();
  119.                 }
  120.         }
  121.  
  122.         /**
  123.          * 解密函数 输入: 要解密的文件,密码(由0-F组成,共48个字符,表示3个8位的密码)如:
  124.          * AD67EA2F3BE6E5ADD368DFE03120B5DF92A8FD8FEC2F0746 其中: AD67EA2F3BE6E5AD
  125.          * DES密码一 D368DFE03120B5DF DES密码二 92A8FD8FEC2F0746 DES密码三 输出:
  126.          * 对输入的文件解密后,保存到用户指定的文件中。
  127.          */
  128.         private void decrypt ( File fileIn, String sKey )
  129.         {
  130.                 try
  131.                 {
  132.                         if ( sKey.length() == 48 )
  133.                         {
  134.                                 String strPath = fileIn.getPath();
  135.                                 if ( strPath.substring ( strPath.length() - 5 ).toLowerCase()
  136.                                         .equals ( ".tdes" ) )
  137.                                         strPath = strPath.substring ( 0, strPath.length() - 5 );
  138.                                 else
  139.                                 {
  140.                                         JOptionPane.showMessageDialog ( this, "不是合法的加密文件!", "提示",
  141.                                                                         JOptionPane.OK_OPTION );
  142.                                         return;
  143.                                 }
  144.                                 JFileChooser chooser = new JFileChooser();
  145.                                 chooser.setCurrentDirectory ( new File ( "." ) );
  146.                                 chooser.setSelectedFile ( new File ( strPath ) );
  147.                                 // 用户指定要保存的文件
  148.                                 int ret = chooser.showSaveDialog ( this );
  149.                                 if ( ret == JFileChooser.APPROVE_OPTION )
  150.                                 {
  151.  
  152.                                         byte[] bytK1 = getKeyByStr ( sKey.substring ( 0, 16 ) );
  153.                                         byte[] bytK2 = getKeyByStr ( sKey.substring ( 16, 32 ) );
  154.                                         byte[] bytK3 = getKeyByStr ( sKey.substring ( 32, 48 ) );
  155.  
  156.                                         FileInputStream fis = new FileInputStream ( fileIn );
  157.                                         byte[] bytIn = new byte[ ( int ) fileIn.length() ];
  158.                                         for ( int i = 0; i < fileIn.length(); i++ )
  159.                                         {
  160.                                                 bytIn[i] = ( byte ) fis.read();
  161.                                         }
  162.                                         // 解密
  163.                                         byte[] bytOut = decryptByDES (
  164.                                                             decryptByDES ( decryptByDES ( bytIn, bytK3 ), bytK2 ),
  165.                                                             bytK1 );
  166.                                         File fileOut = chooser.getSelectedFile();
  167.                                         fileOut.createNewFile();
  168.                                         FileOutputStream fos = new FileOutputStream ( fileOut );
  169.                                         for ( int i = 0; i < bytOut.length; i++ )
  170.                                         {
  171.                                                 fos.write ( ( int ) bytOut[i] );
  172.                                         }
  173.                                         fos.close();
  174.                                         JOptionPane.showMessageDialog ( this, "解密成功!", "提示",
  175.                                                                         JOptionPane.OK_OPTION );
  176.                                 }
  177.                         }
  178.                         else
  179.                                 JOptionPane.showMessageDialog ( this, "密码长度必须等于48!", "错误信息",
  180.                                                                 JOptionPane.ERROR_MESSAGE );
  181.                 }
  182.                 catch ( Exception e )
  183.                 {
  184.                         JOptionPane.showMessageDialog ( this, "解密失败,请核对密码!", "提示",
  185.                                                         JOptionPane.OK_OPTION );
  186.                 }
  187.         }
  188.  
  189.         /**
  190.          * 用DES方法加密输入的字节 bytKey需为8字节长,是加密的密码
  191.          */
  192.         private byte[] encryptByDES ( byte[] bytP, byte[] bytKey ) throws Exception
  193.         {
  194.                 DESKeySpec desKS = new DESKeySpec ( bytKey );
  195.                 SecretKeyFactory skf = SecretKeyFactory.getInstance ( "DES" );
  196.                 SecretKey sk = skf.generateSecret ( desKS );
  197.                 Cipher cip = Cipher.getInstance ( "DES" );
  198.                 cip.init ( Cipher.ENCRYPT_MODE, sk );
  199.                 return cip.doFinal ( bytP );
  200.         }
  201.  
  202.         /**
  203.          * 用DES方法解密输入的字节 bytKey需为8字节长,是解密的密码
  204.          */
  205.         private byte[] decryptByDES ( byte[] bytE, byte[] bytKey ) throws Exception
  206.         {
  207.                 DESKeySpec desKS = new DESKeySpec ( bytKey );
  208.                 SecretKeyFactory skf = SecretKeyFactory.getInstance ( "DES" );
  209.                 SecretKey sk = skf.generateSecret ( desKS );
  210.                 Cipher cip = Cipher.getInstance ( "DES" );
  211.                 cip.init ( Cipher.DECRYPT_MODE, sk );
  212.                 return cip.doFinal ( bytE );
  213.         }
  214.  
  215.         /**
  216.          * 输入密码的字符形式,返回字节数组形式。 如输入字符串:AD67EA2F3BE6E5AD 返回字节数组:{
  217.          * 173,103,234,47,59,230,229,173 }
  218.          */
  219.         private byte[] getKeyByStr ( String str )
  220.         {
  221.                 byte[] bRet = new byte[str.length() / 2];
  222.                 for ( int i = 0; i < str.length() / 2; i++ )
  223.                 {
  224.                         Integer itg = new Integer ( 16 * getChrInt ( str.charAt ( 2 * i ) )
  225.                                                     + getChrInt ( str.charAt ( 2 * i + 1 ) ) );
  226.                         bRet[i] = itg.byteValue();
  227.                 }
  228.                 return bRet;
  229.         }
  230.  
  231.         /**
  232.          * 计算一个16进制字符的10进制值 输入:0-F
  233.          */
  234.         private int getChrInt ( char chr )
  235.         {
  236.                 int iRet = 0;
  237.                 if ( chr == "0".charAt ( 0 ) )
  238.                         iRet = 0;
  239.                 if ( chr == "1".charAt ( 0 ) )
  240.                         iRet = 1;
  241.                 if ( chr == "2".charAt ( 0 ) )
  242.                         iRet = 2;
  243.                 if ( chr == "3".charAt ( 0 ) )
  244.                         iRet = 3;
  245.                 if ( chr == "4".charAt ( 0 ) )
  246.                         iRet = 4;
  247.                 if ( chr == "5".charAt ( 0 ) )
  248.                         iRet = 5;
  249.                 if ( chr == "6".charAt ( 0 ) )
  250.                         iRet = 6;
  251.                 if ( chr == "7".charAt ( 0 ) )
  252.                         iRet = 7;
  253.                 if ( chr == "8".charAt ( 0 ) )
  254.                         iRet = 8;
  255.                 if ( chr == "9".charAt ( 0 ) )
  256.                         iRet = 9;
  257.                 if ( chr == "A".charAt ( 0 ) )
  258.                         iRet = 10;
  259.                 if ( chr == "B".charAt ( 0 ) )
  260.                         iRet = 11;
  261.                 if ( chr == "C".charAt ( 0 ) )
  262.                         iRet = 12;
  263.                 if ( chr == "D".charAt ( 0 ) )
  264.                         iRet = 13;
  265.                 if ( chr == "E".charAt ( 0 ) )
  266.                         iRet = 14;
  267.                 if ( chr == "F".charAt ( 0 ) )
  268.                         iRet = 15;
  269.                 return iRet;
  270.         }
  271. }
  272.  
  273. /**
  274.  * 文件选择组件。
  275.  */
  276. class FilePanel extends JPanel
  277. {
  278.         FilePanel ( String str )
  279.         {
  280.                 JLabel label = new JLabel ( str );
  281.                 JTextField fileText = new JTextField ( 35 );
  282.                 JButton chooseButton = new JButton ( "浏览..." );
  283.                 this.add ( label );
  284.                 this.add ( fileText );
  285.                 this.add ( chooseButton );
  286.                 clickAction ca = new clickAction ( this );
  287.                 chooseButton.addActionListener ( ca );
  288.  
  289.         }
  290.  
  291.         public String getFileName()
  292.         {
  293.                 JTextField jtf = ( JTextField ) this.getComponent ( 1 );
  294.                 return jtf.getText();
  295.         }
  296.  
  297.         private class clickAction implements ActionListener
  298.         {
  299.                 clickAction ( Component c )
  300.                 {
  301.                         cmpt = c;
  302.                 }
  303.  
  304.                 public void actionPerformed ( ActionEvent event )
  305.                 {
  306.                         JFileChooser chooser = new JFileChooser();
  307.                         chooser.setCurrentDirectory ( new File ( "." ) );
  308.                         int ret = chooser.showOpenDialog ( cmpt );
  309.                         if ( ret == JFileChooser.APPROVE_OPTION )
  310.                         {
  311.                                 JPanel jp = ( JPanel ) cmpt;
  312.                                 JTextField jtf = ( JTextField ) jp.getComponent ( 1 );
  313.                                 jtf.setText ( chooser.getSelectedFile().getPath() );
  314.                         }
  315.                 }
  316.  
  317.                 private Component cmpt;
  318.         }
  319. }
  320.  
  321. /**
  322.  * 密码生成组件。
  323.  */
  324. class KeyPanel extends JPanel
  325. {
  326.         KeyPanel ( String str )
  327.         {
  328.                 JLabel label = new JLabel ( str );
  329.                 JTextField fileText = new JTextField ( 35 );
  330.                 JButton chooseButton = new JButton ( "随机产生" );
  331.                 this.add ( label );
  332.                 this.add ( fileText );
  333.                 this.add ( chooseButton );
  334.                 clickAction ca = new clickAction ( this );
  335.                 chooseButton.addActionListener ( ca );
  336.  
  337.         }
  338.  
  339.         // 返回生成的密码(48个字符长度)
  340.         public String getKey()
  341.         {
  342.                 JTextField jtf = ( JTextField ) this.getComponent ( 1 );
  343.                 return jtf.getText();
  344.         }
  345.  
  346.         private class clickAction implements ActionListener
  347.         {
  348.                 clickAction ( Component c )
  349.                 {
  350.                         cmpt = c;
  351.                 }
  352.  
  353.                 public void actionPerformed ( ActionEvent event )
  354.                 {
  355.                         try
  356.                         {
  357.                                 KeyGenerator kg = KeyGenerator.getInstance ( "DES" );
  358.                                 kg.init ( 56 );
  359.                                 Key ke = kg.generateKey();
  360.                                 byte[] bytK1 = ke.getEncoded();
  361.                                 ke = kg.generateKey();
  362.                                 byte[] bytK2 = ke.getEncoded();
  363.                                 ke = kg.generateKey();
  364.                                 byte[] bytK3 = ke.getEncoded();
  365.  
  366.                                 JPanel jp = ( JPanel ) cmpt;
  367.                                 JTextField jtf = ( JTextField ) jp.getComponent ( 1 );
  368.                                 jtf.setText ( getByteStr ( bytK1 ) + getByteStr ( bytK2 )
  369.                                               + getByteStr ( bytK3 ) );
  370.                         }
  371.                         catch ( Exception e )
  372.                         {
  373.                                 e.printStackTrace();
  374.                         }
  375.                 }
  376.  
  377.                 private String getByteStr ( byte[] byt )
  378.                 {
  379.                         String strRet = "";
  380.                         for ( int i = 0; i < byt.length; i++ )
  381.                         {
  382.                                 // System.out.println(byt[i]);
  383.                                 strRet += getHexValue ( ( byt[i] & 240 ) / 16 );
  384.                                 strRet += getHexValue ( byt[i] & 15 );
  385.                         }
  386.                         return strRet;
  387.                 }
  388.  
  389.                 private String getHexValue ( int s )
  390.                 {
  391.                         String sRet = null;
  392.                         switch ( s )
  393.                         {
  394.                         case 0:
  395.                                 sRet = "0";
  396.                                 break;
  397.                         case 1:
  398.                                 sRet = "1";
  399.                                 break;
  400.                         case 2:
  401.                                 sRet = "2";
  402.                                 break;
  403.                         case 3:
  404.                                 sRet = "3";
  405.                                 break;
  406.                         case 4:
  407.                                 sRet = "4";
  408.                                 break;
  409.                         case 5:
  410.                                 sRet = "5";
  411.                                 break;
  412.                         case 6:
  413.                                 sRet = "6";
  414.                                 break;
  415.                         case 7:
  416.                                 sRet = "7";
  417.                                 break;
  418.                         case 8:
  419.                                 sRet = "8";
  420.                                 break;
  421.                         case 9:
  422.                                 sRet = "9";
  423.                                 break;
  424.                         case 10:
  425.                                 sRet = "A";
  426.                                 break;
  427.                         case 11:
  428.                                 sRet = "B";
  429.                                 break;
  430.                         case 12:
  431.                                 sRet = "C";
  432.                                 break;
  433.                         case 13:
  434.                                 sRet = "D";
  435.                                 break;
  436.                         case 14:
  437.                                 sRet = "E";
  438.                                 break;
  439.                         case 15:
  440.                                 sRet = "F";
  441.                         }
  442.                         return sRet;
  443.                 }
  444.  
  445.                 private Component cmpt;
  446.         }
  447. }

回复 "java文本文件加密解密类"

这儿你可以回复上面这条便签

captcha