SecureAES.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.persagy.fm.common.utils;
  2. import cn.hutool.crypto.Mode;
  3. import cn.hutool.crypto.Padding;
  4. import cn.hutool.crypto.SecureUtil;
  5. import cn.hutool.crypto.symmetric.AES;
  6. import com.alibaba.fastjson.JSONObject;
  7. import com.persagy.common.utils.StringUtil;
  8. import com.persagy.fm.common.constant.AppContextConstants;
  9. import com.persagy.security.exception.AESDecryptException;
  10. import java.nio.charset.Charset;
  11. import java.nio.charset.StandardCharsets;
  12. /**
  13. * 借助于hutool工具类实现 AES 128/256加密,全局编码UTF-8
  14. *
  15. * @version 1.0.0
  16. * @author zhangqiankun
  17. * @date 2021-03-13 15:29:50
  18. */
  19. public class SecureAES {
  20. public static void main(String[] args) {
  21. SecureAES aes = new SecureAES("63499E35378AE1B0733E3FED7F780B68", "C0E7BD39B52A15C7");
  22. String encryptAccount = aes.encrypt("TEST", "PC", "AC123456789");
  23. System.out.println("account info encrypt: " + encryptAccount);
  24. JSONObject decrypt = aes.decrypt(encryptAccount + ".FSD45SSD1B5D56GB5DFBD");
  25. System.out.println("header token decrypt: " + decrypt.toJSONString());
  26. JSONObject account = aes.decryptAccount(encryptAccount);
  27. System.out.println("account info decrypt: " + account.toJSONString());
  28. }
  29. private AES aes;
  30. public static final Charset CHARSET_UTF_8 = StandardCharsets.UTF_8;
  31. /**
  32. * 默认AES/ECB/PKCS5Padding
  33. *
  34. * @param key 128或256位
  35. */
  36. public SecureAES (String key) {
  37. aes = SecureUtil.aes(key.getBytes(CHARSET_UTF_8));
  38. }
  39. /**
  40. * 默认AES/CTS/PKCS5Padding
  41. *
  42. * @param key 128或256位
  43. * @param iv 16字节
  44. */
  45. public SecureAES (String key, String iv) {
  46. aes = new AES(Mode.CTS, Padding.PKCS5Padding, key.getBytes(CHARSET_UTF_8), iv.getBytes(CHARSET_UTF_8));
  47. }
  48. /**
  49. * 加密为16进制
  50. *
  51. * @param content
  52. * @return
  53. */
  54. public String encryptHex(String content) {
  55. return aes.encryptHex(content, CHARSET_UTF_8);
  56. }
  57. /**
  58. * 解密
  59. *
  60. * @param content
  61. * @return
  62. */
  63. public String decryptStr(String content) {
  64. return aes.decryptStr(content, CHARSET_UTF_8);
  65. }
  66. /**
  67. * 加密为16进制,参数组装为json格式数据
  68. *
  69. * @param groupCode
  70. * @param appId
  71. * @param accountId
  72. * @return
  73. */
  74. public String encrypt(String groupCode, String appId, String accountId) {
  75. JSONObject object = new JSONObject();
  76. object.put(AppContextConstants.GROUP_CODE, groupCode);
  77. object.put(AppContextConstants.APP_ID, appId);
  78. object.put(AppContextConstants.ACCOUNT_ID, accountId);
  79. return aes.encryptHex(object.toJSONString(), CHARSET_UTF_8);
  80. }
  81. /**
  82. * 解密
  83. *
  84. * @param headerToken token字符串
  85. * @return token内容,json格式
  86. */
  87. public JSONObject decrypt(String headerToken) {
  88. if (StringUtil.isBlank(headerToken)) {
  89. throw new AESDecryptException("token is null");
  90. }
  91. String[] tokens = headerToken.split("\\.");
  92. if (tokens.length != 2) {
  93. throw new AESDecryptException("token invalid parameter");
  94. }
  95. // 加密的账号信息
  96. String encryptAccount = tokens[0];
  97. return this.decryptAccount(encryptAccount);
  98. }
  99. /**
  100. * 解密
  101. *
  102. * @param encryptAccount
  103. * @return token内容,json格式
  104. */
  105. public JSONObject decryptAccount(String encryptAccount) {
  106. String decryptStr = this.decryptStr(encryptAccount);
  107. if (StringUtil.isBlank(decryptStr)) {
  108. throw new AESDecryptException("AES decrypt failure");
  109. }
  110. JSONObject object = JSONObject.parseObject(decryptStr);
  111. return object;
  112. }
  113. }