|
|
|
@ -0,0 +1,101 @@
|
|
|
|
|
package com.baiye.util;
|
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
|
|
|
import javax.crypto.KeyGenerator;
|
|
|
|
|
import javax.crypto.SecretKey;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
|
import java.util.Base64;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* AES加解密
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class AESUtils {
|
|
|
|
|
|
|
|
|
|
/** 密钥长度: 128, 192 or 256 */
|
|
|
|
|
private static final int KEY_SIZE = 128;
|
|
|
|
|
|
|
|
|
|
/** 加密/解密算法名称 */
|
|
|
|
|
private static final String ALGORITHM = "AES";
|
|
|
|
|
|
|
|
|
|
/** 随机数生成器(RNG)算法名称 */
|
|
|
|
|
private static final String RNG_ALGORITHM = "SHA1PRNG";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成密钥对象
|
|
|
|
|
*/
|
|
|
|
|
private static SecretKey generateKey(byte[] key) throws Exception {
|
|
|
|
|
// 创建安全随机数生成器
|
|
|
|
|
SecureRandom random = SecureRandom.getInstance(RNG_ALGORITHM);
|
|
|
|
|
// 设置 密钥key的字节数组 作为安全随机数生成器的种子
|
|
|
|
|
random.setSeed(key);
|
|
|
|
|
|
|
|
|
|
// 创建 AES算法生成器
|
|
|
|
|
KeyGenerator gen = KeyGenerator.getInstance(ALGORITHM);
|
|
|
|
|
// 初始化算法生成器
|
|
|
|
|
gen.init(KEY_SIZE, random);
|
|
|
|
|
|
|
|
|
|
// 生成 AES密钥对象, 也可以直接创建密钥对象: return new SecretKeySpec(key, ALGORITHM);
|
|
|
|
|
return gen.generateKey();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 数据加密: 明文 -> 密文
|
|
|
|
|
*/
|
|
|
|
|
public static String encrypt(String plainBytes, String key) {
|
|
|
|
|
try {
|
|
|
|
|
// 生成密钥对象
|
|
|
|
|
SecretKey secKey = generateKey(key.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
|
|
|
|
|
// 获取 AES 密码器
|
|
|
|
|
Cipher cipher = Cipher.getInstance(ALGORITHM);
|
|
|
|
|
// 初始化密码器(加密模型)
|
|
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, secKey);
|
|
|
|
|
|
|
|
|
|
//数据反转加密
|
|
|
|
|
String s = new StringBuilder(plainBytes).reverse().toString();
|
|
|
|
|
// 加密数据, 返回密文
|
|
|
|
|
byte[] cipherBytes = cipher.doFinal(s.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
|
|
|
|
|
//base64加密
|
|
|
|
|
String str = Base64.getEncoder().encodeToString(cipherBytes);
|
|
|
|
|
return str;
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
log.info("加密失败"+e.fillInStackTrace());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 数据解密: 密文 -> 明文
|
|
|
|
|
*/
|
|
|
|
|
public static String decrypt(String cipherBytes, String key){
|
|
|
|
|
try {
|
|
|
|
|
// 生成密钥对象
|
|
|
|
|
SecretKey secKey = generateKey(key.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
|
|
|
|
|
// 获取 AES 密码器
|
|
|
|
|
Cipher cipher = Cipher.getInstance(ALGORITHM);
|
|
|
|
|
// 初始化密码器(解密模型)
|
|
|
|
|
cipher.init(Cipher.DECRYPT_MODE, secKey);
|
|
|
|
|
|
|
|
|
|
//base64解密
|
|
|
|
|
byte[] bytes = Base64.getDecoder().decode(cipherBytes);
|
|
|
|
|
|
|
|
|
|
// 解密数据, 返回明文
|
|
|
|
|
byte[] plainBytes = cipher.doFinal(bytes);
|
|
|
|
|
|
|
|
|
|
//反转解密的结果返回
|
|
|
|
|
String str = new StringBuilder(new String(plainBytes, "utf-8")).reverse().toString();
|
|
|
|
|
return str;
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
log.info("解密密失败"+e.fillInStackTrace());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|