AesUtils.java 6.9 KB

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