package com.lqkj.link.util; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.lqkj.link.module.authority.domain.UserInfo; import org.bouncycastle.jce.provider.BouncyCastleProvider; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.Security; import java.util.Base64; import java.util.Date; import java.util.Objects; import java.util.Random; public class AESUtils { private static final String ALGORITHM = "AES"; private static final String TRANSFORMATION = "AES/ECB/PKCS7Padding"; static { Security.addProvider(new BouncyCastleProvider()); } public static String encrypt(String plainText, String key) throws Exception { Cipher cipher = Cipher.getInstance(TRANSFORMATION); StringBuilder keyBuilder = new StringBuilder(key); while (keyBuilder.length() < 16) { keyBuilder.append("\0"); } byte[] keyBytes = keyBuilder.toString().getBytes(StandardCharsets.UTF_8); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, ALGORITHM)); byte[] doFinal = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(doFinal); } public static String decrypt(String cipherText, String key) throws Exception { Cipher cipher = Cipher.getInstance(TRANSFORMATION); StringBuilder keyBuilder = new StringBuilder(key); while (keyBuilder.length() < 16) { keyBuilder.append("\0"); } byte[] keyBytes = keyBuilder.toString().getBytes(StandardCharsets.UTF_8); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, ALGORITHM)); byte[] doFinal = cipher.doFinal(Base64.getDecoder().decode(cipherText)); return new String(doFinal); } public static String generateRandomKey(int length) { String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; Random random = new Random(); StringBuilder sb = new StringBuilder(length); for (int i = 0; i < length; i++) { int index = random.nextInt(characters.length()); sb.append(characters.charAt(index)); } return sb.toString(); } public static UserInfo encryptUser(UserInfo userInfo, String key) throws Exception { Date updateTime = userInfo.getUpdateTime(); Date lockTime = userInfo.getLockTime(); JSONObject object = JSON.parseObject(JSON.toJSONString(userInfo)); object.remove("updateTime"); object.remove("lockTime"); JSONArray jsonArray = object.getJSONArray("roleInfoList"); if (jsonArray != null && jsonArray.size() > 0) { for (int i = 0; i < jsonArray.size(); i++) { JSONObject object1 = jsonArray.getJSONObject(i); object1.remove("updateTime"); } } UserInfo encryptUser = JSON.parseObject(JSON.toJSONString(object), UserInfo.class); encryptUser.setUserCode(encrypt(encryptUser.getUserCode(), key)); if (Objects.nonNull(encryptUser.getAuthorizationCode())) encryptUser.setAuthorizationCode(encrypt(encryptUser.getAuthorizationCode(), key)); encryptUser.setUpdateTime(updateTime); encryptUser.setLockTime(lockTime); return encryptUser; } public static void main(String[] args) throws Exception { // String plainText = "123456"; String key = "abc"; // String cipherText = AESUtils.encrypt(plainText, key); String cipherText = "5GzvPLBVRxQC6k37cknLMg=="; System.out.println(cipherText); String plainText = AESUtils.decrypt(cipherText, key); System.out.println(plainText); } }