AesUtils.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package com.template.common.utils;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.bouncycastle.util.encoders.Base64;
  4. import javax.crypto.BadPaddingException;
  5. import javax.crypto.Cipher;
  6. import javax.crypto.IllegalBlockSizeException;
  7. import javax.crypto.NoSuchPaddingException;
  8. import javax.crypto.spec.SecretKeySpec;
  9. import java.security.InvalidKeyException;
  10. import java.security.NoSuchAlgorithmException;
  11. /**
  12. * @Author: liujun
  13. * @Description: Aes 加解密算法
  14. * @Date Create in 上午 9:38$ 2017/12/26 0026$
  15. * @Modify By:
  16. */
  17. @Slf4j
  18. public class AesUtils {
  19. private static String password = "52D04DC20036DBD8";
  20. /**
  21. * @Author liujun
  22. * @Description:
  23. * @params: * @param content 需要加密的内容
  24.  * @param password 加密密码
  25. * @Date 上午 9:41 2017/12/26 0026
  26. */
  27. public static String encrypt(String content) {
  28. if(password.length()<16) {
  29. password = password + "0000000000000000".substring(0, 16-password.length());
  30. }
  31. else if(password.length()>16) {
  32. password = password.substring(0, 16);
  33. }
  34. return bytes2HexString(encryptAES(content.getBytes(), password.getBytes()));
  35. }
  36. /**
  37. * @Author liujun
  38. * @Description:
  39. * @params: * @param content 待解密内容
  40.  * @param password 解密密钥
  41. * @Date 上午 9:40 2017/12/26 0026
  42. */
  43. public static String decrypt(String content) {
  44. if(password.length()<16) {
  45. password = password + "0000000000000000".substring(0, 16-password.length());
  46. }
  47. else if(password.length()>16) {
  48. password = password.substring(0, 16);
  49. }
  50. return new String(decryptAES(hexString2Bytes(content), password.getBytes()));
  51. }
  52. /**
  53. * AES 加密
  54. *
  55. * @param data 明文
  56. * @param key 16、24、32 字节秘钥
  57. * @return 密文
  58. */
  59. public static byte[] encryptAES(final byte[] data, final byte[] key) {
  60. try {
  61. SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
  62. Cipher cipher = Cipher.getInstance("AES");// 创建密码器
  63. byte[] byteContent = data;
  64. cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);// 初始化
  65. byte[] result = cipher.doFinal(byteContent);
  66. return result; // 加密
  67. } catch (NoSuchAlgorithmException e) {
  68. log.error(e.getMessage(),e);
  69. } catch (NoSuchPaddingException e) {
  70. log.error(e.getMessage(),e);
  71. } catch (InvalidKeyException e) {
  72. log.error(e.getMessage(),e);
  73. } catch (IllegalBlockSizeException e) {
  74. log.error(e.getMessage(),e);
  75. } catch (BadPaddingException e) {
  76. log.error(e.getMessage(),e);
  77. } catch (Exception e) {
  78. log.error(e.getMessage(),e);
  79. }
  80. return null;
  81. }
  82. /**
  83. * AES 解密
  84. *
  85. * @param data 密文
  86. * @param key 16、24、32 字节秘钥
  87. * @return 明文
  88. */
  89. public static byte[] decryptAES(final byte[] data, final byte[] key) {
  90. try {
  91. SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
  92. Cipher cipher = Cipher.getInstance("AES");// 创建密码器
  93. cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);// 初始化
  94. byte[] result = cipher.doFinal(data);
  95. return result; // 加密
  96. } catch (NoSuchAlgorithmException e) {
  97. log.error(e.getMessage(),e);
  98. } catch (NoSuchPaddingException e) {
  99. log.error(e.getMessage(),e);
  100. } catch (InvalidKeyException e) {
  101. log.error(e.getMessage(),e);
  102. } catch (IllegalBlockSizeException e) {
  103. log.error(e.getMessage(),e);
  104. } catch (BadPaddingException e) {
  105. log.error(e.getMessage(),e);
  106. } catch (Exception e) {
  107. log.error(e.getMessage(),e);
  108. }
  109. return null;
  110. }
  111. public static String bytes2HexString(final byte[] bytes) {
  112. if (bytes == null) return null;
  113. int len = bytes.length;
  114. if (len <= 0) return null;
  115. char[] ret = new char[len << 1];
  116. for (int i = 0, j = 0; i < len; i++) {
  117. ret[j++] = hexDigits[bytes[i] >>> 4 & 0x0f];
  118. ret[j++] = hexDigits[bytes[i] & 0x0f];
  119. }
  120. return new String(ret);
  121. }
  122. private static final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  123. public static byte[] hexString2Bytes(String hexString) {
  124. if (isSpace(hexString)) return null;
  125. int len = hexString.length();
  126. if (len % 2 != 0) {
  127. hexString = "0" + hexString;
  128. len = len + 1;
  129. }
  130. char[] hexBytes = hexString.toUpperCase().toCharArray();
  131. byte[] ret = new byte[len >> 1];
  132. for (int i = 0; i < len; i += 2) {
  133. ret[i >> 1] = (byte) (hex2Dec(hexBytes[i]) << 4 | hex2Dec(hexBytes[i + 1]));
  134. }
  135. return ret;
  136. }
  137. private static int hex2Dec(final char hexChar) {
  138. if (hexChar >= '0' && hexChar <= '9') {
  139. return hexChar - '0';
  140. } else if (hexChar >= 'A' && hexChar <= 'F') {
  141. return hexChar - 'A' + 10;
  142. } else {
  143. throw new IllegalArgumentException();
  144. }
  145. }
  146. public static byte[] base64Encode(final byte[] input) {
  147. return Base64.encode(input);
  148. }
  149. public static byte[] base64Decode(final byte[] input) {
  150. return Base64.decode(input);
  151. }
  152. private static boolean isSpace(final String s) {
  153. if (s == null) return true;
  154. for (int i = 0, len = s.length(); i < len; ++i) {
  155. if (!Character.isWhitespace(s.charAt(i))) {
  156. return false;
  157. }
  158. }
  159. return true;
  160. }
  161. public static void main(String[] args) {
  162. String s="{\n" +
  163. " \"categoryId\": \"72cf28a8789643bbbbb62d08ee91f17e\",\n" +
  164. " \"luid\": \"80A036D93CFB\",\n" +
  165. " \"type\":\"4\",\n" +
  166. " \"userName\":\"13097286670\",\n" +
  167. " \"startTime\":\"1682389484000\",\n" +
  168. " \"endTime\":\"1684981484000\",\n" +
  169. " \"password\":\"A08E87B5E777EBEE2C6EF3262F069D5A\"\n" +
  170. "}";
  171. //加密
  172. String encryptString = AesUtils.encrypt("548903");
  173. logger.info("加密后字符串:"+encryptString);
  174. //解密
  175. String decryptString = AesUtils.decrypt(encryptString);
  176. logger.info("解密后字符串:"+decryptString);
  177. }
  178. }