Java实现DES加密解密工具类

信息的加密可分为对称和非对称两种方式,前者表示加密后的信息可以解密成原值,而后者则不能根据加密后的信息还原为原值。MD5属于非对称加密,而DES属于对称加密,下面介绍一下DES工具类。

  1. package com.app;    
  2.     
  3. import java.security.Key;    
  4. import java.security.Security;    
  5. import javax.crypto.Cipher;    
  6. /**  
  7.  * DES加密和解密工具,可以对字符串进行加密和解密操作  。   
  8.  */    
  9. public class Test4 {    
  10.     /**  
  11.      * 默认构造方法,使用默认密钥  
  12.      */    
  13.     public Test4() throws Exception {    
  14.         this(strDefaultKey);    
  15.     }    
  16.     /**  
  17.      * 指定密钥构造方法  
  18.      * @param strKey  指定的密钥  
  19.      * @throws Exception  
  20.      */    
  21.     public Test4(String strKey) throws Exception {    
  22.         // Security.addProvider(new com.sun.crypto.provider.SunJCE());    
  23.         Key key = getKey(strKey.getBytes());    
  24.         encryptCipher = Cipher.getInstance("DES");    
  25.         encryptCipher.init(Cipher.ENCRYPT_MODE, key);    
  26.         decryptCipher = Cipher.getInstance("DES");    
  27.         decryptCipher.init(Cipher.DECRYPT_MODE, key);    
  28.     }    
  29.     /** 字符串默认键值 */    
  30.     private static String strDefaultKey = "national";    
  31.     /** 加密工具 */    
  32.     private Cipher encryptCipher = null;    
  33.     /** 解密工具 */    
  34.     private Cipher decryptCipher = null;    
  35.     /**  
  36.      * 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[]  
  37.      * hexStr2ByteArr(String strIn) 互为可逆的转换过程  
  38.      * @param arrB  需要转换的byte数组  
  39.      * @return 转换后的字符串  
  40.      * @throws Exception 本方法不处理任何异常,所有异常全部抛出  
  41.      */    
  42.     public static String byteArr2HexStr(byte[] arrB) throws Exception {    
  43.         int iLen = arrB.length;    
  44.         // 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍    
  45.         StringBuffer sb = new StringBuffer(iLen * 2);    
  46.         for (int i = 0; i < iLen; i++) {    
  47.             int intTmp = arrB[i];    
  48.             // 把负数转换为正数    
  49.             while (intTmp < 0) {    
  50.                 intTmp = intTmp + 256;    
  51.             }    
  52.             // 小于0F的数需要在前面补0    
  53.             if (intTmp < 16) {    
  54.                 sb.append("0");    
  55.             }    
  56.             sb.append(Integer.toString(intTmp, 16));    
  57.         }    
  58.         return sb.toString();    
  59.     }    
  60.     /**  
  61.      * 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)  
  62.      * 互为可逆的转换过程  
  63.      * @param strIn 需要转换的字符串  
  64.      * @return 转换后的byte数组  
  65.      */    
  66.     public static byte[] hexStr2ByteArr(String strIn) throws Exception {    
  67.         byte[] arrB = strIn.getBytes();    
  68.         int iLen = arrB.length;    
  69.         // 两个字符表示一个字节,所以字节数组长度是字符串长度除以2    
  70.         byte[] arrOut = new byte[iLen / 2];    
  71.         for (int i = 0; i < iLen; i = i + 2) {    
  72.             String strTmp = new String(arrB, i, 2);    
  73.             arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);    
  74.         }    
  75.         return arrOut;    
  76.     }    
  77.     /**  
  78.      * 加密字节数组  
  79.      * @param arrB  需加密的字节数组  
  80.      * @return 加密后的字节数组  
  81.      */    
  82.     public byte[] encrypt(byte[] arrB) throws Exception {    
  83.         return encryptCipher.doFinal(arrB);    
  84.     }    
  85.     /**  
  86.      * 加密字符串  
  87.      * @param strIn  需加密的字符串  
  88.      * @return 加密后的字符串  
  89.      */    
  90.     public String encrypt(String strIn) throws Exception {    
  91.         return byteArr2HexStr(encrypt(strIn.getBytes()));    
  92.     }    
  93.     /**  
  94.      * 解密字节数组  
  95.      * @param arrB  需解密的字节数组  
  96.      * @return 解密后的字节数组  
  97.      */    
  98.     public byte[] decrypt(byte[] arrB) throws Exception {    
  99.         return decryptCipher.doFinal(arrB);    
  100.     }    
  101.     /**  
  102.      * 解密字符串  
  103.      * @param strIn  需解密的字符串  
  104.      * @return 解密后的字符串  
  105.      */    
  106.     public String decrypt(String strIn) throws Exception {    
  107.         return new String(decrypt(hexStr2ByteArr(strIn)));    
  108.     }    
  109.     /**  
  110.      * 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位  
  111.      * @param arrBTmp  构成该字符串的字节数组  
  112.      * @return 生成的密钥  
  113.      */    
  114.     private Key getKey(byte[] arrBTmp) throws Exception {    
  115.         // 创建一个空的8位字节数组(默认值为0)    
  116.         byte[] arrB = new byte[8];    
  117.         // 将原始字节数组转换为8位    
  118.         for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {    
  119.             arrB[i] = arrBTmp[i];    
  120.         }    
  121.         // 生成密钥    
  122.         Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");    
  123.         return key;    
  124.     }    
  125.     public static void main(String[] args) {    
  126.         try {    
  127.             String test1 = "987654321";    
  128.             Test4 des1 = new Test4();// 使用默认密钥    
  129.             System.out.println("加密前的字符:" + test1);    
  130.             System.out.println("加密后的字符:" + des1.encrypt(test1));    
  131.             System.out.println("解密后的字符:" + des1.decrypt(des1.encrypt(test1)));    
  132.                 
  133.             String test2 = "123456789";    
  134.             Test4 des2 = new Test4("leeme32nz");// 自定义密钥    
  135.             System.out.println("加密前的字符:" + test2);    
  136.             System.out.println("加密后的字符:" + des2.encrypt(test2));    
  137.             System.out.println("解密后的字符:" + des2.decrypt(des2.encrypt(test2)));    
  138.         } catch (Exception e) {    
  139.             e.printStackTrace();    
  140.         }    
  141.     }    
  142. }    

运行的结果

  1. 加密前的字符:987654321    
  2. 加密后的字符:5579cb6ffef7b473e6b92e28c43dccd4    
  3. 解密后的字符:987654321    
  4.     
  5. 加密前的字符:123456789    
  6. 加密后的字符:beeaf4d9ae0b2b5153a02d9feb0f6a9c    
  7. 解密后的字符:123456789   


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值