package com.ahysf.common.utils;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @ClassName: AESUtil
* @author: 〆、dyh
* @since: 2022/3/3 10:38
*/
public class AESUtil {
private static final String KEY_ALGORITHM = "AES";
// 算法/模式/补码方式
private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS7Padding";
private static final int KEY_LENGTH = 192;
static {
try {
Security.addProvider(new BouncyCastleProvider());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成aes密钥
*
* @param
* @return java.lang.String
* @MethodName: generateAesKey
* @author: 〆、dyh
* @since: 2022/3/4 10:24
*/
public static String generateAesKey() {
try {
KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
//下面调用方法的参数决定了生成密钥的长度,可以修改为128,192,256
//192 32
//128 24
//256 44
kg.init(KEY_LENGTH);
//生成密钥
SecretKey sk = kg.generateKey();
//返回密钥的二进制编码
byte[] b = sk.getEncoded();
return Base64.encodeBase64String(b);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException("没有此算法");
}
}
/**
* 加密
*
* @param sSrc
* @param sKey
* @return java.lang.String
* @MethodName: encrypt
* @author: 〆、dyh
* @since: 2022/3/4 10:24
*/
public static String encrypt(String sSrc, String sKey) throws Exception {
if (StringUtils.isBlank(sKey)) {
System.out.print("Key为空");
return null;
}
// 判断Key是否为32位
if (sKey.length() != 32) {
System.out.print("Key长度不是32位");
return null;
}
KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);
kgen.init(KEY_LENGTH, new SecureRandom(sKey.getBytes()));
SecretKey secretKey = kgen.generateKey();
//获得密码的字节数组
byte[] enCodeFormat = secretKey.getEncoded();
//根据密码生成AES密钥
SecretKeySpec skeySpec = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);
//根据指定算法CIPHER_ALGORITHM制成密码器
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); // 算法/模式/补码方式
//初始化密码器,第一个参数为加密(ENCRYPT_MODE)或者解密(DECRYPT_MODE)操作,第二个参数为生成的AES密钥
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
//密码器加密数据
byte[] encrypted = cipher.doFinal(sSrc.getBytes(StandardCharsets.UTF_8));
//将加密后的数据转换为字符串返回
return new Base64().encodeToString(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。
}
/**
* 解密
*
* @param sSrc
* @param sKey
* @return java.lang.String
* @MethodName: encrypt
* @author: 〆、dyh
* @since: 2022/3/4 10:24
*/
public static String decrypt(String sSrc, String sKey) throws Exception {
try {
// 判断Key是否正确
if (sKey == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为32位
if (sKey.length() != 32) {
System.out.print("Key长度不是32位");
return null;
}
KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);
kgen.init(KEY_LENGTH, new SecureRandom(sKey.getBytes()));
SecretKey secretKey = kgen.generateKey();
//获得密码的字节数组
byte[] enCodeFormat = secretKey.getEncoded();
//根据密码生成AES密钥
SecretKeySpec skeySpec = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);
//根据指定算法CIPHER_ALGORITHM制成密码器
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); // 算法/模式/补码方式
//初始化密码器,第一个参数为加密(ENCRYPT_MODE)或者解密(DECRYPT_MODE)操作,第二个参数为生成的AES密钥
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
//把密文字符串转回密文字节数组
byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密
try {
//密码器解密数据
byte[] original = cipher.doFinal(encrypted1);
//将解密后的数据转换为字符串返回
return new String(original, StandardCharsets.UTF_8);
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
public static void main(String[] args) throws Exception {
/*
* 此处使用AES-192-ECB加密模式,key需要为32位。
*/
String cKey = generateAesKey();
// 需要加密的字符串
String cSrc = "我爱你啊";
System.out.println("需要加密的字符串:" + cSrc);
System.out.println("需要加密的字符串长度:" + cSrc.length());
// 加密
String enString = encrypt(cSrc, cKey);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
System.out.println("时间:" + simpleDateFormat.format(new Date()) + "加密后的字串是:" + enString);
// 解密
String DeString = decrypt(enString, cKey);
System.out.println("时间:" + simpleDateFormat.format(new Date()) + "解密后的字串是:" + DeString);
}
}
AESUtil
于 2022-03-15 11:30:33 首次发布