package com.chinaunicom.standard.product.ucenter.utils;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.UUID;
/**
* @author ggw
*/
public class AESUtils {
private static final String KEY_ALGORITHM = "AES";
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";//默认的加密算法
private static final String IV_STRING = "ABCDEF1234123412";
private static final String UTF8 = "utf-8";
private static String AES_KEY = null;
public static String MOBILE_AES_KEY = "825ef073c3ac4186";
private static String generateKey() {
AES_KEY = UUID.randomUUID().toString().replaceAll("-", "");
if (AES_KEY.length() > 16) {
AES_KEY = AES_KEY.substring(0, 16);
}
return AES_KEY;
}
public static String getAesKey() {
if (checkAesKey()) {
return AES_KEY;
} else {
return generateKey();
}
}
public static boolean checkAesKey() {
if (AES_KEY == null) {
return false;
} else {
return true;
}
}
public static String encrypt(String content) {
return encrypt(content, getAesKey());
}
public static String encrypt(String content, String key) {
try {
byte[] byteContent = content.getBytes(UTF8);
//这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成
byte[] enCodeFormat = key.getBytes(UTF8);
//指定加密的算法、工作模式和填充方式
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);
byte[] initParam = IV_STRING.getBytes(UTF8);
IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] encryptedBytes = cipher.doFinal(byteContent);
//对加密后数据进行 base64 编码
return Base64.encodeToString(encryptedBytes, Base64.NO_WRAP);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String decrypt(String content) {
return decrypt(content, getAesKey());
}
public static String decrypt(String content, String key) {
try {
// base64 解码
byte[] encryptedBytes = Base64.decode(content, Base64.NO_WRAP);
byte[] enCodeFormat = key.getBytes(UTF8);
//指定加密的算法、工作模式和填充方式
SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);
byte[] initParam = IV_STRING.getBytes(UTF8);
IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
byte[] result = cipher.doFinal(encryptedBytes);
return new String(result, UTF8);
} catch (Exception e) {
}
return null;
}
public static String encrypt(String content, String key,boolean useIvParameterSpec) {
try {
byte[] byteContent = content.getBytes(UTF8);
//这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成
byte[] enCodeFormat = key.getBytes(UTF8);
//指定加密的算法、工作模式和填充方式
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);
byte[] initParam = IV_STRING.getBytes(UTF8);
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
if (useIvParameterSpec){
IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
}else{
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
}
byte[] encryptedBytes = cipher.doFinal(byteContent);
//对加密后数据进行 base64 编码
return Base64.encodeToString(encryptedBytes, Base64.NO_WRAP);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
AES加解密
最新推荐文章于 2025-05-20 22:23:34 发布