加密解密工具类 EncryptUtil

  1. import java.io.IOException;  
  2. import java.io.UnsupportedEncodingException;  
  3. import java.security.MessageDigest;  
  4.   
  5. import javax.crypto.Cipher;  
  6. import javax.crypto.spec.IvParameterSpec;  
  7. import javax.crypto.spec.SecretKeySpec;  
  8.   
  9. import sun.misc.BASE64Decoder;  
  10. import sun.misc.BASE64Encoder;  
  11.   
  12. /** 
  13.  * 功能描述 
  14.  * 加密常用类 
  15.  */  
  16. public class EncryptUtil {  
  17.     // 密钥是16位长度的byte[]进行Base64转换后得到的字符串  
  18.     public static String key = "LmMGStGtOpF4xNyvYt54EQ==";  
  19.   
  20.     /** 
  21.      * <li> 
  22.      * 方法名称:encrypt</li> <li> 
  23.      * 加密方法 
  24.      * @param xmlStr 
  25.      *            需要加密的消息字符串 
  26.      * @return 加密后的字符串 
  27.      */  
  28.     public static String encrypt(String xmlStr) {  
  29.         byte[] encrypt = null;  
  30.   
  31.         try {  
  32.             // 取需要加密内容的utf-8编码。  
  33.             encrypt = xmlStr.getBytes("utf-8");  
  34.         } catch (UnsupportedEncodingException e) {  
  35.             e.printStackTrace();  
  36.         }  
  37.         // 取MD5Hash码,并组合加密数组  
  38.         byte[] md5Hasn = null;  
  39.         try {  
  40.             md5Hasn = EncryptUtil.MD5Hash(encrypt, 0, encrypt.length);  
  41.         } catch (Exception e) {  
  42.             e.printStackTrace();  
  43.         }  
  44.         // 组合消息体  
  45.         byte[] totalByte = EncryptUtil.addMD5(md5Hasn, encrypt);  
  46.   
  47.         // 取密钥和偏转向量  
  48.         byte[] key = new byte[8];  
  49.         byte[] iv = new byte[8];  
  50.         getKeyIV(EncryptUtil.key, key, iv);  
  51.         SecretKeySpec deskey = new SecretKeySpec(key, "DES");  
  52.         IvParameterSpec ivParam = new IvParameterSpec(iv);  
  53.   
  54.         // 使用DES算法使用加密消息体  
  55.         byte[] temp = null;  
  56.         try {  
  57.             temp = EncryptUtil.DES_CBC_Encrypt(totalByte, deskey, ivParam);  
  58.         } catch (Exception e) {  
  59.             e.printStackTrace();  
  60.         }  
  61.   
  62.         // 使用Base64加密后返回  
  63.         return new BASE64Encoder().encode(temp);  
  64.     }  
  65.   
  66.     /** 
  67.      * <li> 
  68.      * 方法名称:encrypt</li> <li> 
  69.      * 功能描述: 
  70.      *  
  71.      * <pre> 
  72.      * 解密方法 
  73.      * </pre> 
  74.      *  
  75.      * </li> 
  76.      *  
  77.      * @param xmlStr 
  78.      *            需要解密的消息字符串 
  79.      * @return 解密后的字符串 
  80.      * @throws Exception 
  81.      */  
  82.     public static String decrypt(String xmlStr) throws Exception {  
  83.         // base64解码  
  84.         BASE64Decoder decoder = new BASE64Decoder();  
  85.         byte[] encBuf = null;  
  86.         try {  
  87.             encBuf = decoder.decodeBuffer(xmlStr);  
  88.         } catch (IOException e) {  
  89.             e.printStackTrace();  
  90.         }  
  91.   
  92.         // 取密钥和偏转向量  
  93.         byte[] key = new byte[8];  
  94.         byte[] iv = new byte[8];  
  95.         getKeyIV(EncryptUtil.key, key, iv);  
  96.   
  97.         SecretKeySpec deskey = new SecretKeySpec(key, "DES");  
  98.         IvParameterSpec ivParam = new IvParameterSpec(iv);  
  99.   
  100.         // 使用DES算法解密  
  101.         byte[] temp = null;  
  102.         try {  
  103.             temp = EncryptUtil.DES_CBC_Decrypt(encBuf, deskey, ivParam);  
  104.         } catch (Exception e) {  
  105.             e.printStackTrace();  
  106.         }  
  107.   
  108.         // 进行解密后的md5Hash校验  
  109.         byte[] md5Hash = null;  
  110.         try {  
  111.             md5Hash = EncryptUtil.MD5Hash(temp, 16, temp.length - 16);  
  112.         } catch (Exception e) {  
  113.             e.printStackTrace();  
  114.         }  
  115.   
  116.         // 进行解密校检  
  117.         for (int i = 0; i < md5Hash.length; i++) {  
  118.             if (md5Hash[i] != temp[i]) {  
  119.                 // System.out.println(md5Hash[i] + "MD5校验错误。" + temp[i]);  
  120.                 throw new Exception("MD5校验错误。");  
  121.             }  
  122.         }  
  123.   
  124.         // 返回解密后的数组,其中前16位MD5Hash码要除去。  
  125.         return new String(temp, 16, temp.length - 16"utf-8");  
  126.     }  
  127.   
  128.     /** 
  129.      * <li> 
  130.      * 方法名称:TripleDES_CBC_Encrypt</li> <li> 
  131.      * 功能描述: 
  132.      *  
  133.      * <pre> 
  134.      * 经过封装的三重DES/CBC加密算法,如果包含中文,请注意编码。 
  135.      * </pre> 
  136.      *  
  137.      * </li> 
  138.      *  
  139.      * @param sourceBuf 
  140.      *            需要加密内容的字节数组。 
  141.      * @param deskey 
  142.      *            KEY 由24位字节数组通过SecretKeySpec类转换而成。 
  143.      * @param ivParam 
  144.      *            IV偏转向量,由8位字节数组通过IvParameterSpec类转换而成。 
  145.      * @return 加密后的字节数组 
  146.      * @throws Exception 
  147.      */  
  148.     public static byte[] TripleDES_CBC_Encrypt(byte[] sourceBuf,  
  149.             SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception {  
  150.         byte[] cipherByte;  
  151.         // 使用DES对称加密算法的CBC模式加密  
  152.         Cipher encrypt = Cipher.getInstance("TripleDES/CBC/PKCS5Padding");  
  153.   
  154.         encrypt.init(Cipher.ENCRYPT_MODE, deskey, ivParam);  
  155.   
  156.         cipherByte = encrypt.doFinal(sourceBuf, 0, sourceBuf.length);  
  157.         // 返回加密后的字节数组  
  158.         return cipherByte;  
  159.     }  
  160.   
  161.     /** 
  162.      * <li> 
  163.      * 方法名称:TripleDES_CBC_Decrypt</li> <li> 
  164.      * 功能描述: 
  165.      *  
  166.      * <pre> 
  167.      * 经过封装的三重DES / CBC解密算法 
  168.      * </pre> 
  169.      *  
  170.      * </li> 
  171.      *  
  172.      * @param sourceBuf 
  173.      *            需要解密内容的字节数组 
  174.      * @param deskey 
  175.      *            KEY 由24位字节数组通过SecretKeySpec类转换而成。 
  176.      * @param ivParam 
  177.      *            IV偏转向量,由6位字节数组通过IvParameterSpec类转换而成。 
  178.      * @return 解密后的字节数组 
  179.      * @throws Exception 
  180.      */  
  181.     public static byte[] TripleDES_CBC_Decrypt(byte[] sourceBuf,  
  182.             SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception {  
  183.   
  184.         byte[] cipherByte;  
  185.         // 获得Cipher实例,使用CBC模式。  
  186.         Cipher decrypt = Cipher.getInstance("TripleDES/CBC/PKCS5Padding");  
  187.         // 初始化加密实例,定义为解密功能,并传入密钥,偏转向量  
  188.         decrypt.init(Cipher.DECRYPT_MODE, deskey, ivParam);  
  189.   
  190.         cipherByte = decrypt.doFinal(sourceBuf, 0, sourceBuf.length);  
  191.         // 返回解密后的字节数组  
  192.         return cipherByte;  
  193.     }  
  194.   
  195.     /** 
  196.      * <li> 
  197.      * 方法名称:DES_CBC_Encrypt</li> <li> 
  198.      * 功能描述: 
  199.      *  
  200.      * <pre> 
  201.      * 经过封装的DES/CBC加密算法,如果包含中文,请注意编码。 
  202.      * </pre> 
  203.      *  
  204.      * </li> 
  205.      *  
  206.      * @param sourceBuf 
  207.      *            需要加密内容的字节数组。 
  208.      * @param deskey 
  209.      *            KEY 由8位字节数组通过SecretKeySpec类转换而成。 
  210.      * @param ivParam 
  211.      *            IV偏转向量,由8位字节数组通过IvParameterSpec类转换而成。 
  212.      * @return 加密后的字节数组 
  213.      * @throws Exception 
  214.      */  
  215.     public static byte[] DES_CBC_Encrypt(byte[] sourceBuf,  
  216.             SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception {  
  217.         byte[] cipherByte;  
  218.         // 使用DES对称加密算法的CBC模式加密  
  219.         Cipher encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");  
  220.   
  221.         encrypt.init(Cipher.ENCRYPT_MODE, deskey, ivParam);  
  222.   
  223.         cipherByte = encrypt.doFinal(sourceBuf, 0, sourceBuf.length);  
  224.         // 返回加密后的字节数组  
  225.         return cipherByte;  
  226.     }  
  227.   
  228.     /** 
  229.      * <li> 
  230.      * 方法名称:DES_CBC_Decrypt</li> <li> 
  231.      * 功能描述: 
  232.      *  
  233.      * <pre> 
  234.      * 经过封装的DES/CBC解密算法。 
  235.      * </pre> 
  236.      *  
  237.      * </li> 
  238.      *  
  239.      * @param sourceBuf 
  240.      *            需要解密内容的字节数组 
  241.      * @param deskey 
  242.      *            KEY 由8位字节数组通过SecretKeySpec类转换而成。 
  243.      * @param ivParam 
  244.      *            IV偏转向量,由6位字节数组通过IvParameterSpec类转换而成。 
  245.      * @return 解密后的字节数组 
  246.      * @throws Exception 
  247.      */  
  248.     public static byte[] DES_CBC_Decrypt(byte[] sourceBuf,  
  249.             SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception {  
  250.   
  251.         byte[] cipherByte;  
  252.         // 获得Cipher实例,使用CBC模式。  
  253.         Cipher decrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");  
  254.         // 初始化加密实例,定义为解密功能,并传入密钥,偏转向量  
  255.         decrypt.init(Cipher.DECRYPT_MODE, deskey, ivParam);  
  256.   
  257.         cipherByte = decrypt.doFinal(sourceBuf, 0, sourceBuf.length);  
  258.         // 返回解密后的字节数组  
  259.         return cipherByte;  
  260.     }  
  261.   
  262.     /** 
  263.      * <li> 
  264.      * 方法名称:MD5Hash</li> <li> 
  265.      * 功能描述: 
  266.      *  
  267.      * <pre> 
  268.      * MD5,进行了简单的封装,以适用于加,解密字符串的校验。 
  269.      * </pre> 
  270.      *  
  271.      * </li> 
  272.      *  
  273.      * @param buf 
  274.      *            需要MD5加密字节数组。 
  275.      * @param offset 
  276.      *            加密数据起始位置。 
  277.      * @param length 
  278.      *            需要加密的数组长度。 
  279.      * @return 
  280.      * @throws Exception 
  281.      */  
  282.     public static byte[] MD5Hash(byte[] buf, int offset, int length)  
  283.             throws Exception {  
  284.         MessageDigest md = MessageDigest.getInstance("MD5");  
  285.         md.update(buf, offset, length);  
  286.         return md.digest();  
  287.     }  
  288.   
  289.     /** 
  290.      * <li> 
  291.      * 方法名称:byte2hex</li> <li> 
  292.      * 功能描述: 
  293.      *  
  294.      * <pre> 
  295.      * 字节数组转换为二行制表示 
  296.      * </pre> 
  297.      *  
  298.      * </li> 
  299.      *  
  300.      * @param inStr 
  301.      *            需要转换字节数组。 
  302.      * @return 字节数组的二进制表示。 
  303.      */  
  304.     public static String byte2hex(byte[] inStr) {  
  305.         String stmp;  
  306.         StringBuffer out = new StringBuffer(inStr.length * 2);  
  307.   
  308.         for (int n = 0; n < inStr.length; n++) {  
  309.             // 字节做"与"运算,去除高位置字节 11111111  
  310.             stmp = Integer.toHexString(inStr[n] & 0xFF);  
  311.             if (stmp.length() == 1) {  
  312.                 // 如果是0至F的单位字符串,则添加0  
  313.                 out.append("0" + stmp);  
  314.             } else {  
  315.                 out.append(stmp);  
  316.             }  
  317.         }  
  318.         return out.toString();  
  319.     }  
  320.   
  321.     /** 
  322.      * <li> 
  323.      * 方法名称:addMD5</li> <li> 
  324.      * 功能描述: 
  325.      *  
  326.      * <pre> 
  327.      * MD校验码 组合方法,前16位放MD5Hash码。 把MD5验证码byte[],加密内容byte[]组合的方法。 
  328.      * </pre> 
  329.      *  
  330.      * </li> 
  331.      *  
  332.      * @param md5Byte 
  333.      *            加密内容的MD5Hash字节数组。 
  334.      * @param bodyByte 
  335.      *            加密内容字节数组 
  336.      * @return 组合后的字节数组,比加密内容长16个字节。 
  337.      */  
  338.     public static byte[] addMD5(byte[] md5Byte, byte[] bodyByte) {  
  339.         int length = bodyByte.length + md5Byte.length;  
  340.         byte[] resutlByte = new byte[length];  
  341.   
  342.         // 前16位放MD5Hash码  
  343.         for (int i = 0; i < length; i++) {  
  344.             if (i < md5Byte.length) {  
  345.                 resutlByte[i] = md5Byte[i];  
  346.             } else {  
  347.                 resutlByte[i] = bodyByte[i - md5Byte.length];  
  348.             }  
  349.         }  
  350.   
  351.         return resutlByte;  
  352.     }  
  353.   
  354.     /** 
  355.      * <li> 
  356.      * 方法名称:getKeyIV</li> <li> 
  357.      * 功能描述: 
  358.      *  
  359.      * <pre> 
  360.      *  
  361.      * </pre> 
  362.      * </li> 
  363.      *  
  364.      * @param encryptKey 
  365.      * @param key 
  366.      * @param iv 
  367.      */  
  368.     public static void getKeyIV(String encryptKey, byte[] key, byte[] iv) {  
  369.         // 密钥Base64解密  
  370.         BASE64Decoder decoder = new BASE64Decoder();  
  371.         byte[] buf = null;  
  372.         try {  
  373.             buf = decoder.decodeBuffer(encryptKey);  
  374.         } catch (IOException e) {  
  375.             e.printStackTrace();  
  376.         }  
  377.         // 前8位为key  
  378.         int i;  
  379.         for (i = 0; i < key.length; i++) {  
  380.             key[i] = buf[i];  
  381.         }  
  382.         // 后8位为iv向量  
  383.         for (i = 0; i < iv.length; i++) {  
  384.             iv[i] = buf[i + 8];  
  385.         }  
  386.     }  
  387.       
  388.     public static void main(String[] args) {  
  389.         System.out.println(encrypt("123456"));  
  390.     }  
  391. }  
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值