123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- 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);
- }
- }
|