博客引用处(以下内容在原有博客基础上进行补充或更改,谢谢这些大牛的博客指导):
JAVA实现AES加密
加密代码
/**
* 加密
*
* @param content 需要加密的内容
* @param password 加密密码
* @return
*/
public static byte[] encrypt(String content, String password) {
try {
KeyGenerator kgen = ("AES");
(128, new SecureRandom(()));
SecretKey secretKey = ();
byte[] enCodeFormat = ();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = ("AES");// 创建密码器
byte[] byteContent = ("utf-8");
(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = (byteContent);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
();
} catch (NoSuchPaddingException e) {
();
} catch (InvalidKeyException e) {
();
} catch (UnsupportedEncodingException e) {
();
} catch (IllegalBlockSizeException e) {
();
} catch (BadPaddingException e) {
();
}
return null;
}
解密代码
/**解密
* 解密的时候要传入byte数组
* @param content 待解密内容
* @param password 解密密钥
* @return
*/
public static byte[] decrypt(byte[] content, String password) {
try {
KeyGenerator kgen = ("AES");
(128, new SecureRandom(()));
SecretKey secretKey = ();
byte[] enCodeFormat = ();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = ("AES");// 创建密码器
(Cipher.DECRYPT_MODE, key);// 初始化
byte[] result = (content);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
();
} catch (NoSuchPaddingException e) {
();
} catch (InvalidKeyException e) {
();
} catch (IllegalBlockSizeException e) {
();
} catch (BadPaddingException e) {
();
}
return null;
}
测试代码
String content = "test";
String password = "12345678";
//加密
("加密前:" + content);
byte[] encryptResult = encrypt(content, password);
//解密
byte[] decryptResult = decrypt(encryptResult,password);
("解密后:" + new String(decryptResult));
输出结果如下:
加密前:test
解密后:test
容易出错的地方:
如果我们将测试代码修改一下,如下:
String content = "test";
String password = "12345678";
//加密
("加密前:" + content);
byte[] encryptResult = encrypt(content, password);
try {
String encryptResultStr = new String(encryptResult,"utf-8");
//解密
byte[] decryptResult = decrypt(("utf-8"),password);
("解密后:" + new String(decryptResult));
} catch (UnsupportedEncodingException e) {
();
}
则,系统会报出如下异常:
: Input length must be multiple of 16 when decrypting with padded cipher
at .SunJCE_f.b(DashoA13*..)
at .SunJCE_f.b(DashoA13*..)
at (DashoA13*..)
at (DashoA13*..)
这主要是因为加密后的byte数组是不能强制转换成字符串的,换言之:字符串和byte数组在这种情况下不是互逆的;要避免这种情况,我们需要做一些修订,可以考虑将二进制数据转换成十六进制表示,主要有如下两个方法:
二进制转换成16进制
/**将二进制转换成16进制
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < ; i++) {
String hex = (buf[i] & 0xFF);
if (() == 1) {
hex = '0' + hex;
}
(());
}
return ();
}
16进制转换为二进制
/**将16进制转换为二进制
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (() < 1)
return null;
byte[] result = new byte[()/2];
for (int i = 0;i< ()/2; i++) {
int high = ((i*2, i*2+1), 16);
int low = ((i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
然后,我们再修订以上测试代码,如下:
String content = "test";
String password = "12345678";
//加密
("加密前:" + content);
byte[] encryptResult = encrypt(content, password);
String encryptResultStr = parseByte2HexStr(encryptResult);
("加密后:" + encryptResultStr);
//解密
byte[] decryptFrom = parseHexStr2Byte(encryptResultStr);
byte[] decryptResult = decrypt(decryptFrom,password);
("解密后:" + new String(decryptResult));
测试结果如下:
加密前:test
加密后:73C58BAFE578C59366D8C995CD0B9D6D
解密后:test
另外一种加密方式
/**
* 加密
*
* @param content 需要加密的内容
* @param password 加密密码
* @return
*/
public static byte[] encrypt2(String content, String password) {
try {
SecretKeySpec key = new SecretKeySpec((), "AES");
Cipher cipher = ("AES/ECB/NoPadding");
byte[] byteContent = ("utf-8");
(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = (byteContent);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
();
} catch (NoSuchPaddingException e) {
();
} catch (InvalidKeyException e) {
();
} catch (UnsupportedEncodingException e) {
();
} catch (IllegalBlockSizeException e) {
();
} catch (BadPaddingException e) {
();
}
return null;
}
这种加密方式有两种限制
密钥必须是16位的
待加密内容的长度必须是16的倍数,如果不是16的倍数,就会出如下异常:
: Input length not multiple of 16 bytes
at .SunJCE_f.a(DashoA13*..)
at .SunJCE_f.b(DashoA13*..)
at .SunJCE_f.b(DashoA13*..)
at (DashoA13*..)
at (DashoA13*..)
要解决如上异常,可以通过补全传入加密内容等方式进行避免。