/**
* AES加密实现方法
*/
public static byte[] AESEncrypt(byte[] plainBytes, byte[] keyBytes, String keyAlgorithm, String cipherAlgorithm, String IV)
throws Exception {
try {
// AES密钥长度为128bit、192bit、256bit,默认为128bit
if (keyBytes.length % 8 != 0 || keyBytes.length < 16 || keyBytes.length > 32) {
throw new Exception("AES密钥长度不合法");
}
Cipher cipher = Cipher.getInstance(cipherAlgorithm);
SecretKey secretKey = new SecretKeySpec(keyBytes, keyAlgorithm);
if (StringUtils.trimToNull(IV) != null) {
IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
} else {
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
}
byte[] encryptedBytes = cipher.doFinal(plainBytes);
return encryptedBytes;
} catch (NoSuchAlgorithmException e) {
throw new Exception(String.format("没有[%s]此类加密算法", cipherAlgorithm));
} catch (NoSuchPaddingException e) {
throw new Exception(String.format("没有[%s]此类填充模式", cipherAlgorithm));
} catch (InvalidKeyException e) {
System.out.println(e);
throw new Exception("无效密钥");
} catch (InvalidAlgorithmParameterException e) {
throw new Exception("无效密钥参数");
} catch (BadPaddingException e) {
throw new Exception("错误填充模式");
} catch (IllegalBlockSizeException e) {
throw new Exception("加密块大小不合法");
}
}
/**
* AES解密实现方法
*/
public static byte[] AESDecrypt(byte[] encryptedBytes, byte[] keyBytes, String keyAlgorithm, String cipherAlgorithm, String IV)
throws Exception {
try {
// AES密钥长度为128bit、192bit、256bit,默认为128bit
if (keyBytes.length % 8 != 0 || keyBytes.length < 16 || keyBytes.length > 32) {
throw new Exception("AES密钥长度不合法");
}
Cipher cipher = Cipher.getInstance(cipherAlgorithm);
SecretKey secretKey = new SecretKeySpec(keyBytes, keyAlgorithm);
if (IV != null && StringUtils.trimToNull(IV) != null) {
IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes());
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
} else {
cipher.init(Cipher.DECRYPT_MODE, secretKey);
}
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return decryptedBytes;
} catch (NoSuchAlgorithmException e) {
throw new Exception(String.format("没有[%s]此类加密算法", cipherAlgorithm));
} catch (NoSuchPaddingException e) {
throw new Exception(String.format("没有[%s]此类填充模式", cipherAlgorithm));
} catch (InvalidKeyException e) {
throw new Exception("无效密钥");
} catch (InvalidAlgorithmParameterException e) {
throw new Exception("无效密钥参数");
} catch (BadPaddingException e) {
throw new Exception("错误填充模式");
} catch (IllegalBlockSizeException e) {
throw new Exception("解密块大小不合法");
}
}
/**
* AES加密
* @author zc-t208
* @param param 需要加密的字符串
* @return
*/
public static String AESEncodeParam(String param){
if(StringUtils.isEmpty(param)){
return null;
}
String encryptData = "";
String strKey = sysConfigService.getAESStrKey().getConfigValue();
try {
byte[] base64encryptDataBytes = Base64.encodeBase64(AESEncrypt(param.getBytes("UTF-8"),strKey.getBytes("UTF-8"),"AES","AES/ECB/PKCS5Padding",null));
encryptData = new String(base64encryptDataBytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
log.info(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
log.info(e.getMessage());
e.printStackTrace();
}
return encryptData;
}
/**
* AES解密
* @author zc-t208
* @param param 需要解密的参数
* @return
*/
public static String AESdecodeParam(String param){
if(StringUtils.isEmpty(param)){
return null;
}
String resData = "";
String strKey = sysConfigService.getAESStrKey().getConfigValue();
try {
byte[] decodeBase64DataBytes = Base64.decodeBase64(param.getBytes("UTF-8"));
byte[] merchantXmlDataBytes = AESDecrypt(decodeBase64DataBytes,strKey.getBytes("UTF-8"), "AES", "AES/ECB/PKCS5Padding", null);
resData = new String(merchantXmlDataBytes,"UTF-8");
} catch (UnsupportedEncodingException e) {
log.info(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
log.info(e.getMessage());
e.printStackTrace();
}
return resData;
}
/**
* 获取AES秘钥方法
* @param length
* @return
*/
public static String generateLenString(int length) {
char[] cResult = new char[length];
int[] flag = { 0, 0, 0 }; // A-Z, a-z, 0-9
int i = 0;
while (flag[0] == 0 || flag[1] == 0 || flag[2] == 0 || i < length) {
i = i % length;
int f = (int) (Math.random() * 3 % 3);
if (f == 0)
cResult[i] = (char) ('A' + Math.random() * 26);
else if (f == 1)
cResult[i] = (char) ('a' + Math.random() * 26);
else
cResult[i] = (char) ('0' + Math.random() * 10);
flag[f] = 1;
i++;
}
return new String(cResult);
}
注意:加密秘钥为16位随机数字,并且加密和解密使用同一秘钥,加密后的参数想要传递,需要再进行一次加密后再传输
keyAlgorithm为"AES"
cipherAlgorithm为"AES/ECB/PKCS5Padding"
本博客为自己记录所做,如果有什么不对的地方,请多多指教