MD5.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package com.iamberry.app.tool.des;
  2. import java.security.Key;
  3. import java.security.MessageDigest;
  4. import java.security.Security;
  5. import javax.crypto.Cipher;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Component;
  8. import com.iamberry.app.tool.log.RatFWLogger;
  9. /**
  10. * MD5处理类
  11. * 注意:
  12. * 1、在加密是不许要创建对象,但是在解密时必须创建对象
  13. * 2、如果在加密时使用的秘钥和解密时使用的秘钥不符合,那么解析不正确,抛出异常:javax.crypto.BadPaddingException
  14. * @author 何秀刚
  15. */
  16. @Component
  17. public class MD5 {
  18. @Autowired
  19. private RatFWLogger logger;
  20. public void setInLongLogger(RatFWLogger logger) {
  21. this.logger = logger;
  22. }
  23. // md5字符数组
  24. private char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7',
  25. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  26. /**
  27. * 将一个字符串转换为32位的字符串
  28. */
  29. public String stringToMD5(String src) throws Exception {
  30. StringBuffer md5Str = new StringBuffer();
  31. byte[] bytes = src.getBytes();
  32. MessageDigest digest = MessageDigest.getInstance("MD5");
  33. byte[] newBytes = digest.digest(bytes);
  34. for (byte b : newBytes) {
  35. md5Str.append(chars[b >> 4 & 0x0F]);
  36. md5Str.append(chars[b & 0x0F]);
  37. }
  38. return md5Str.toString();
  39. }
  40. /** 字符串默认键值 */
  41. public static String strDefaultKey = "RATFWMD5";
  42. /** 加密工具 */
  43. public Cipher encryptCipher = null;
  44. /** 解密工具 */
  45. public Cipher decryptCipher = null;
  46. /**
  47. * 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public byte[]
  48. * hexStr2ByteArr(String strIn) 互为可逆的转换过程
  49. */
  50. public String byteArr2HexStr(byte[] arrB) throws Exception {
  51. int iLen = arrB.length;
  52. // 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
  53. StringBuffer sb = new StringBuffer(iLen * 2);
  54. for (int i = 0; i < iLen; i++) {
  55. int intTmp = arrB[i];
  56. // 把负数转换为正数
  57. while (intTmp < 0) {
  58. intTmp = intTmp + 256;
  59. }
  60. // 小于0F的数需要在前面补0
  61. if (intTmp < 16) {
  62. sb.append("0");
  63. }
  64. sb.append(Integer.toString(intTmp, 16));
  65. }
  66. return sb.toString();
  67. }
  68. /**
  69. * 将表示16进制值的字符串转换为byte数组, 和public String byteArr2HexStr(byte[] arrB)
  70. * 互为可逆的转换过程
  71. */
  72. public byte[] hexStr2ByteArr(String strIn) throws Exception {
  73. byte[] arrB = strIn.getBytes();
  74. int iLen = arrB.length;
  75. // 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
  76. byte[] arrOut = new byte[iLen / 2];
  77. for (int i = 0; i < iLen; i = i + 2) {
  78. String strTmp = new String(arrB, i, 2);
  79. arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
  80. }
  81. return arrOut;
  82. }
  83. /**
  84. * 默认构造方法,使用默认密钥
  85. *
  86. * @throws Exception
  87. */
  88. public MD5() {
  89. this(strDefaultKey);
  90. }
  91. /**
  92. * 指定密钥构造方法
  93. *
  94. * @param strKey
  95. * 指定的密钥
  96. * @throws Exception
  97. */
  98. public MD5(String strKey) {
  99. try {
  100. Security.addProvider(new com.sun.crypto.provider.SunJCE());
  101. Key key = getKey(strKey.getBytes());
  102. encryptCipher = Cipher.getInstance("DES");
  103. encryptCipher.init(Cipher.ENCRYPT_MODE, key);
  104. decryptCipher = Cipher.getInstance("DES");
  105. decryptCipher.init(Cipher.DECRYPT_MODE, key);
  106. } catch (Exception e) {
  107. }
  108. }
  109. /**
  110. * 加密字节数组
  111. */
  112. public byte[] encrypt(byte[] arrB) throws Exception {
  113. return encryptCipher.doFinal(arrB);
  114. }
  115. /**
  116. * 加密字符串
  117. */
  118. public String encrypt(String strIn) throws Exception {
  119. try {
  120. return byteArr2HexStr(encrypt(strIn.getBytes())).toUpperCase();
  121. } catch (Exception e) {
  122. e.printStackTrace();
  123. return null;
  124. }
  125. }
  126. /**
  127. * 解密字节数组
  128. */
  129. public byte[] decrypt(byte[] arrB) throws Exception {
  130. return decryptCipher.doFinal(arrB);
  131. }
  132. /**
  133. * 解密字符串
  134. */
  135. public String decrypt(String strIn) throws Exception {
  136. try {
  137. return new String(decrypt(hexStr2ByteArr(strIn)));
  138. } catch (Exception e) {
  139. logger.debug("加密和解密字符串不匹配:", e.getMessage());
  140. return "INLONGADMD5";
  141. }
  142. }
  143. /**
  144. * 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
  145. */
  146. private Key getKey(byte[] arrBTmp) throws Exception {
  147. // 创建一个空的8位字节数组(默认值为0)
  148. byte[] arrB = new byte[8];
  149. // 将原始字节数组转换为8位
  150. for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
  151. arrB[i] = arrBTmp[i];
  152. }
  153. // 生成密钥
  154. Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
  155. return key;
  156. }
  157. }