123 lines
3.6 KiB
Java
Raw Normal View History

2023-02-16 14:17:36 +08:00
package com.zhgd.mybatis;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* aes 加密的工具类
* 1.存储 加密的秘钥key
* 2.实现 aes 加密
* 3.实现aes解密的功能
*
* @author 邱平毅
*/
@Slf4j
public class Aes {
public Aes() {
}
/**
* 定义 aes 加密的key
* 密钥 必须是16位, 自定义,
* 如果不是16位, 则会出现InvalidKeyException: Illegal key size
* 解决方案有两种
* 1.需要安装Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files(可以在Oracle下载).
* 2.设置设置key的长度为16个字母和数字的字符窜128 Bit/8=16字符就不报错了
*/
private static final String KEY = "ZHGDzhdgJXJADMIN";
/**
* 定义加密的编码
*/
private static final String CHARSET = "utf-8";
/**
* 偏移量
*/
private static final int OFFSET = 16;
2023-06-28 20:32:50 +08:00
// private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
2023-02-16 14:17:36 +08:00
private static final String ALGORITHM = "AES";
/**
* 加密
*
* @param content
* @return
*/
public static String encrypt(String content) {
return encrypt(content, KEY);
}
/**
* 解密
*
* @param content
* @return
*/
public static String decrypt(String content) {
return decrypt(content, KEY);
}
/**
* 加密
*
* @param content 需要加密的内容
* @param key 加密密码
* @return
*/
public static String encrypt(String content, String key) {
try {
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), ALGORITHM);
IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, OFFSET);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
byte[] byteContent = content.getBytes(CHARSET);
// 初始化
2023-06-28 20:32:50 +08:00
cipher.init(Cipher.ENCRYPT_MODE, skey);
2023-02-16 14:17:36 +08:00
byte[] result = cipher.doFinal(byteContent);
// 加密
return new Base64().encodeToString(result);
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
}
/**
* AES256解密
*
* @param content 待解密内容
* @param key 解密密钥
* @return 解密之后
*/
public static String decrypt(String content, String key) {
try {
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), ALGORITHM);
IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, OFFSET);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
// 初始化
2023-06-28 20:32:50 +08:00
cipher.init(Cipher.DECRYPT_MODE, skey);
2023-02-16 14:17:36 +08:00
byte[] result = cipher.doFinal(new Base64().decode(content));
// 解密
2023-09-07 11:44:57 +08:00
return new String(result, "UTF-8");
2023-02-16 14:17:36 +08:00
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
}
2023-06-28 20:32:50 +08:00
public static void main(String[] args) {
2023-09-07 11:44:57 +08:00
String content = "grWn9TcbYmxu0KjA5utvyX38gGTPleC86M5PM8CuFmnqBaSNY0UV2xjn4EyijUZdkQtOPmES+ElFMTEXaiWo0Q==";
2023-07-03 09:31:37 +08:00
// System.out.println("RaxMRsBUfFDjz/U/7LB/TBRIDE/1I6ZZ9kmQFWqgLCID42evqzeC8kpDSd3GM7YB".replace("+", "-").replace("/", "_"));
// System.out.println(encrypt("{\"uid\":\"test\",\"exp\":1686559180478}", "ssologin66!@#$%^"));
2023-09-07 11:44:57 +08:00
System.out.println(decrypt(content, KEY));
2023-06-28 20:32:50 +08:00
}
2023-02-16 14:17:36 +08:00
}