package com.yh.watercloud.authentication.utils;
import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class SymmetricEncryptionUtil {
private SymmetricEncryptionUtil() {
}
public static final String KEY = "默认秘钥";
public static AES AES_AL = SecureUtil.aes(Base64.decode(KEY));
/**
* 默认秘钥加密
*
* @param data
* @return
*/
public static String enCode(String data) {
return AES_AL.encryptBase64(data, "utf-8");
}
/**
* 默认秘钥解密
*
* @param data
* @return
*/
public static String deCode(String data) {
return AES_AL.decryptStr(data);
}
/**
* 生成秘钥
*
* @return
*/
public static String generateKey() {
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
return Base64.encode(key);
}
/**
* 指定秘钥加密
*
* @param key
* @param content
* @return
*/
public static String enCode(String key, String content) {
AES aes = SecureUtil.aes(Base64.decode(key));
return aes.encryptBase64(content);
}
/**
* 指定秘钥解密
*
* @param key
* @param content
* @return
*/
public static String deCode(String key, String content) {
AES aes = SecureUtil.aes(Base64.decode(key));
return aes.decryptStr(content);
}
// public static void main(String[] args) {
// System.out.println(SymmetricEncryptionUtil.enCode("您好!!!!"));
// System.out.println(SymmetricEncryptionUtil.deCode(SymmetricEncryptionUtil.enCode("您好!!!!")));
//
// String key = SymmetricEncryptionUtil.generateKey();
// String content = "您好!";
//
// String enStr = SymmetricEncryptionUtil.enCode(key, content);
// String deStr = SymmetricEncryptionUtil.deCode(key, enStr);
// log.info("key:{}", key);
// log.info("enStr:{}", enStr);
// log.info("deStr:{}", deStr);
// }
}