AESUtils.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package com.lqkj.link.util;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONArray;
  4. import com.alibaba.fastjson2.JSONObject;
  5. import com.lqkj.link.module.authority.domain.UserInfo;
  6. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  7. import javax.crypto.Cipher;
  8. import javax.crypto.spec.SecretKeySpec;
  9. import java.nio.charset.StandardCharsets;
  10. import java.security.Security;
  11. import java.util.Base64;
  12. import java.util.Date;
  13. import java.util.Objects;
  14. import java.util.Random;
  15. public class AESUtils {
  16. private static final String ALGORITHM = "AES";
  17. private static final String TRANSFORMATION = "AES/ECB/PKCS7Padding";
  18. static {
  19. Security.addProvider(new BouncyCastleProvider());
  20. }
  21. public static String encrypt(String plainText, String key) throws Exception {
  22. Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  23. StringBuilder keyBuilder = new StringBuilder(key);
  24. while (keyBuilder.length() < 16) {
  25. keyBuilder.append("\0");
  26. }
  27. byte[] keyBytes = keyBuilder.toString().getBytes(StandardCharsets.UTF_8);
  28. cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, ALGORITHM));
  29. byte[] doFinal = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
  30. return Base64.getEncoder().encodeToString(doFinal);
  31. }
  32. public static String decrypt(String cipherText, String key) throws Exception {
  33. Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  34. StringBuilder keyBuilder = new StringBuilder(key);
  35. while (keyBuilder.length() < 16) {
  36. keyBuilder.append("\0");
  37. }
  38. byte[] keyBytes = keyBuilder.toString().getBytes(StandardCharsets.UTF_8);
  39. cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, ALGORITHM));
  40. byte[] doFinal = cipher.doFinal(Base64.getDecoder().decode(cipherText));
  41. return new String(doFinal);
  42. }
  43. public static String generateRandomKey(int length) {
  44. String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  45. Random random = new Random();
  46. StringBuilder sb = new StringBuilder(length);
  47. for (int i = 0; i < length; i++) {
  48. int index = random.nextInt(characters.length());
  49. sb.append(characters.charAt(index));
  50. }
  51. return sb.toString();
  52. }
  53. public static UserInfo encryptUser(UserInfo userInfo, String key) throws Exception {
  54. Date updateTime = userInfo.getUpdateTime();
  55. Date lockTime = userInfo.getLockTime();
  56. JSONObject object = JSON.parseObject(JSON.toJSONString(userInfo));
  57. object.remove("updateTime");
  58. object.remove("lockTime");
  59. JSONArray jsonArray = object.getJSONArray("roleInfoList");
  60. if (jsonArray != null && jsonArray.size() > 0) {
  61. for (int i = 0; i < jsonArray.size(); i++) {
  62. JSONObject object1 = jsonArray.getJSONObject(i);
  63. object1.remove("updateTime");
  64. }
  65. }
  66. UserInfo encryptUser = JSON.parseObject(JSON.toJSONString(object), UserInfo.class);
  67. encryptUser.setUserCode(encrypt(encryptUser.getUserCode(), key));
  68. if (Objects.nonNull(encryptUser.getAuthorizationCode())) encryptUser.setAuthorizationCode(encrypt(encryptUser.getAuthorizationCode(), key));
  69. encryptUser.setUpdateTime(updateTime);
  70. encryptUser.setLockTime(lockTime);
  71. return encryptUser;
  72. }
  73. public static void main(String[] args) throws Exception {
  74. // String plainText = "123456";
  75. String key = "abc";
  76. // String cipherText = AESUtils.encrypt(plainText, key);
  77. String cipherText = "5GzvPLBVRxQC6k37cknLMg==";
  78. System.out.println(cipherText);
  79. String plainText = AESUtils.decrypt(cipherText, key);
  80. System.out.println(plainText);
  81. }
  82. }