AESEncyptUtil.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package org.dbsyncer.common.util;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import javax.crypto.Cipher;
  5. import javax.crypto.KeyGenerator;
  6. import javax.crypto.SecretKey;
  7. import javax.crypto.spec.SecretKeySpec;
  8. import java.io.UnsupportedEncodingException;
  9. import java.security.NoSuchAlgorithmException;
  10. import java.security.SecureRandom;
  11. import java.util.Arrays;
  12. public abstract class AESEncyptUtil {
  13. private final static Logger logger = LoggerFactory.getLogger(AESEncyptUtil.class);
  14. // 默认缺省种子
  15. public static String keySeed = "c32ad1415f6c89fee76d8457c31efb4b";
  16. /**
  17. * 加密
  18. *
  19. * @param content
  20. * 需要加密的内容
  21. * @param keyseed
  22. * 密钥
  23. * @return
  24. */
  25. public static String encrypt(String content, String keyseed) throws Exception {
  26. KeyGenerator kgen = KeyGenerator.getInstance("AES");
  27. // 解决linux环境下密码解密问题
  28. SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
  29. secureRandom.setSeed(keyseed.getBytes("UTF-8"));
  30. kgen.init(128, secureRandom);
  31. SecretKey secretKey = kgen.generateKey();
  32. byte[] enCodeFormat = secretKey.getEncoded();
  33. SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
  34. Cipher cipher = Cipher.getInstance("AES");// 创建密码器
  35. byte[] byteContent = content.getBytes("utf-8");
  36. cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
  37. byte[] result = cipher.doFinal(byteContent);
  38. return byte2Hex(result); // 加密
  39. }
  40. /**
  41. * 解密
  42. *
  43. * @param content
  44. * 待解密内容
  45. * @param keyseed
  46. * 解密密钥
  47. * @return
  48. */
  49. public static byte[] decrypt(byte[] content, String keyseed) throws Exception {
  50. KeyGenerator kgen = KeyGenerator.getInstance("AES");
  51. // 解决linux环境下密码解密问题
  52. SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
  53. secureRandom.setSeed(keyseed.getBytes("UTF-8"));
  54. kgen.init(128, secureRandom);
  55. SecretKey secretKey = kgen.generateKey();
  56. byte[] enCodeFormat = secretKey.getEncoded();
  57. SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
  58. Cipher cipher = Cipher.getInstance("AES");// 创建密码器
  59. cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
  60. byte[] result = cipher.doFinal(hex2Byte(content));
  61. return result; // 加密
  62. }
  63. /**
  64. * 解密
  65. *
  66. * @param content
  67. * 待解密内容
  68. * @param keyseed
  69. * 解密密钥
  70. * @return
  71. */
  72. public static String decrypt(String content, String keyseed) throws Exception {
  73. KeyGenerator kgen = KeyGenerator.getInstance("AES");
  74. // 解决linux环境下密码解密问题
  75. SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
  76. secureRandom.setSeed(keyseed.getBytes("UTF-8"));
  77. kgen.init(128, secureRandom);
  78. SecretKey secretKey = kgen.generateKey();
  79. byte[] enCodeFormat = secretKey.getEncoded();
  80. SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
  81. Cipher cipher = Cipher.getInstance("AES");// 创建密码器
  82. cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
  83. String result = new String(cipher.doFinal(hex2Byte(content.getBytes("UTF-8"))), "UTF-8");
  84. return result.trim();
  85. }
  86. /**
  87. * 对于js加密
  88. *
  89. * @param content
  90. * 内容
  91. * @param keyseed
  92. * 解密密钥
  93. * @return
  94. * @throws Exception
  95. */
  96. public static String encryptForJS(String content, String keyseed) throws Exception {
  97. SecretKeySpec key = getKeySpecFromBytes(keyseed.toUpperCase());
  98. Cipher cipher = Cipher.getInstance("AES");
  99. cipher.init(Cipher.ENCRYPT_MODE, key);
  100. byte[] byteEnc = cipher.doFinal(content.getBytes("UTF-8"));
  101. return byte2Hex(byteEnc);
  102. }
  103. /**
  104. * 解密
  105. *
  106. * @param content
  107. * 待解密内容
  108. * @param keyseed
  109. * 解密密钥
  110. * @return
  111. */
  112. public static String decryptForJS(String content, String keyseed) throws Exception {
  113. SecretKeySpec key = getKeySpecFromBytes(keyseed.toUpperCase());
  114. Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
  115. cipher.init(Cipher.DECRYPT_MODE, key);
  116. String result = new String(cipher.doFinal(hex2Byte(content.getBytes("UTF-8"))), "UTF-8");
  117. return result.trim();
  118. }
  119. private static String byte2Hex(byte[] b) {
  120. String hs = "";
  121. String stmp = "";
  122. for (int n = 0; n < b.length; n++) {
  123. stmp = Integer.toHexString(b[n] & 0xFF);
  124. if (stmp.length() == 1) {
  125. hs += "0" + stmp;
  126. } else {
  127. hs += stmp;
  128. }
  129. }
  130. return hs.toUpperCase();
  131. }
  132. /**
  133. * 十六进制字符串到字节转换
  134. *
  135. * @param b
  136. * byte类型的数组
  137. * @return
  138. */
  139. private static byte[] hex2Byte(byte[] b) {
  140. if ((b.length % 2) != 0) {
  141. throw new IllegalArgumentException("长度不是偶数!");
  142. }
  143. byte[] b2 = new byte[b.length / 2];
  144. for (int n = 0; n < b.length; n += 2) {
  145. try {
  146. String item = null;
  147. // 判断n值是否在b字节长度范围之内,否则,造成堆内存溢出
  148. if (n + 2 <= b.length) {
  149. item = new String(b, n, 2, "UTF-8");
  150. b2[n / 2] = (byte) Integer.parseInt(item, 16);
  151. }
  152. item = "";
  153. item = null;
  154. } catch (UnsupportedEncodingException e) {
  155. logger.error(e.getMessage(), e);
  156. }
  157. }
  158. // 在垃圾回收延迟的情况下,进行记忆清楚,避免信息被窃取
  159. byte temp = 0;
  160. // 将字节数组赋值为0,删除原有数据
  161. Arrays.fill(b, temp);
  162. return b2;
  163. }
  164. /**
  165. * 从十六进制字符串生成Key
  166. *
  167. * @param strBytes
  168. * str字节
  169. * @return
  170. * @throws NoSuchAlgorithmException
  171. */
  172. private static SecretKeySpec getKeySpecFromBytes(String strBytes) throws NoSuchAlgorithmException {
  173. SecretKeySpec spec = null;
  174. try {
  175. spec = new SecretKeySpec(hex2Byte(strBytes.getBytes("UTF-8")), "AES");
  176. } catch (UnsupportedEncodingException e) {
  177. logger.error(e.getMessage(), e);
  178. }
  179. try {
  180. spec = new SecretKeySpec(hex2Byte(strBytes.getBytes("UTF-8")), "AES");
  181. } catch (UnsupportedEncodingException e) {
  182. logger.error(e.getMessage(), e);
  183. }
  184. return spec;
  185. }
  186. }