package com.dy.common.util;
|
|
import javax.crypto.Cipher;
|
import javax.crypto.spec.SecretKeySpec;
|
import java.util.Base64;
|
|
/**
|
* @author ZhuBaoMin
|
* @date 2024-10-23 14:32
|
* @LastEditTime 2024-10-23 14:32
|
* @Description AES对称加密解密
|
*/
|
public class AES {
|
private static final String ALGORITHM = "AES";
|
private static final String TRANSFORMATION = "AES";
|
|
// 16-byte secret key
|
private static final String SECRET_KEY = "YanJiuYuanSecret";
|
|
public static String encrypt(String input) throws Exception {
|
SecretKeySpec keySpec = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
|
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
|
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
|
|
byte[] encryptedBytes = cipher.doFinal(input.getBytes());
|
return Base64.getEncoder().encodeToString(encryptedBytes);
|
}
|
|
public static String decrypt(String input) throws Exception {
|
SecretKeySpec keySpec = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
|
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
|
cipher.init(Cipher.DECRYPT_MODE, keySpec);
|
|
byte[] decodedBytes = Base64.getDecoder().decode(input);
|
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
|
return new String(decryptedBytes);
|
}
|
}
|