package com.ruoyi.im.util;
|
|
import cn.hutool.crypto.Mode;
|
import cn.hutool.crypto.Padding;
|
import cn.hutool.crypto.symmetric.AES;
|
|
import javax.crypto.spec.IvParameterSpec;
|
import javax.crypto.spec.SecretKeySpec;
|
import java.nio.charset.StandardCharsets;
|
|
/**
|
* @ClassName : SymmetricCryptoUtil //类名
|
* @Description : 对称加密工具类 //描述
|
* @Author : tf //作者
|
* @Date: 2022/10/23 23:31
|
*/
|
public class SymmetricCryptoUtil {
|
|
/**
|
* 16字节的密钥(128位)
|
*/
|
private static final String ENCODE_KEY = "03SsJLwB03SsJLwB"; // 改为16字节
|
|
/**
|
* 16字节的初始化向量(128位)
|
*/
|
private static final String IV_KEY = "0000000000000000"; // 改为16个0
|
|
public static void main(String[] args) {
|
String originalText = "123456";
|
|
String encryptData = encryptFromString(originalText, Mode.CBC, Padding.ZeroPadding);
|
System.out.println("加密:" + encryptData);
|
String decryptData = decryptFromString("LbPnKi4WWPQ2NllKmCkAwg==", Mode.CBC, Padding.ZeroPadding);
|
System.out.println("解密:" + decryptData);
|
|
// 验证加解密是否一致
|
System.out.println("验证结果:" + originalText.equals(decryptData));
|
}
|
|
/**
|
* 加密字符串
|
*/
|
public static String encryptFromString(String data, Mode mode, Padding padding) {
|
AES aes;
|
if (Mode.CBC == mode) {
|
aes = new AES(mode, padding,
|
new SecretKeySpec(ENCODE_KEY.getBytes(StandardCharsets.UTF_8), "AES"),
|
new IvParameterSpec(IV_KEY.getBytes(StandardCharsets.UTF_8)));
|
} else {
|
aes = new AES(mode, padding,
|
new SecretKeySpec(ENCODE_KEY.getBytes(StandardCharsets.UTF_8), "AES"));
|
}
|
return aes.encryptBase64(data, StandardCharsets.UTF_8);
|
}
|
|
/**
|
* 解密字符串
|
*/
|
public static String decryptFromString(String data, Mode mode, Padding padding) {
|
AES aes;
|
if (Mode.CBC == mode) {
|
aes = new AES(mode, padding,
|
new SecretKeySpec(ENCODE_KEY.getBytes(StandardCharsets.UTF_8), "AES"),
|
new IvParameterSpec(IV_KEY.getBytes(StandardCharsets.UTF_8)));
|
} else {
|
aes = new AES(mode, padding,
|
new SecretKeySpec(ENCODE_KEY.getBytes(StandardCharsets.UTF_8), "AES"));
|
}
|
byte[] decryptDataBase64 = aes.decrypt(data);
|
return new String(decryptDataBase64, StandardCharsets.UTF_8);
|
}
|
|
/**
|
* @Description: 加密密码(使用CBC模式和ZeroPadding)
|
* @Param: password 明文密码
|
* @return: 加密后的Base64字符串
|
* @Author: tf
|
* @Date: 2022/10/23
|
*/
|
public static String encryptPassword(String password) {
|
return encryptFromString(password, Mode.CBC, Padding.ZeroPadding);
|
}
|
|
/**
|
* @Description: 解密密码
|
* @Param: encryptedPassword 加密后的密码字符串
|
* @return: 解密后的明文密码
|
* @Author: tf
|
* @Date: 2022/10/23
|
*/
|
public static String decryptPassword(String encryptedPassword) {
|
return decryptFromString(encryptedPassword, Mode.CBC, Padding.ZeroPadding);
|
}
|
|
/**
|
* 生成随机的16字节密钥(用于生产环境)
|
*/
|
public static String generateRandomKey() {
|
java.security.SecureRandom random = new java.security.SecureRandom();
|
byte[] key = new byte[16];
|
random.nextBytes(key);
|
return new String(key, StandardCharsets.UTF_8);
|
}
|
|
/**
|
* 生成随机的16字节IV(用于生产环境)
|
*/
|
public static String generateRandomIV() {
|
java.security.SecureRandom random = new java.security.SecureRandom();
|
byte[] iv = new byte[16];
|
random.nextBytes(iv);
|
return new String(iv, StandardCharsets.UTF_8);
|
}
|
}
|