AES加密解密过程中,由于是在jetty服务下开发的,运行中文不乱码,但是在测试在tomcat下还是出现了中文乱码(已经在server. 配过了utf-8编码格式),然后就是一系列转码过程,在这过程中知道,gbk转utf-8乱造成字节流失,造成奇数中文奇数乱码,最后解决方法是解密字节码时就转码,相对应的加密的时候也要转码:
/**
* AES解密
* @param encryptBytes 待解密的byte[]
* @param decryptKey 解密密钥
* @return 解密后的String
* @throws Exception
*/
public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
byte[] decryptBytes = cipher.doFinal(encryptBytes);
return new String(decryptBytes,"utf-8"); //此处需转码
}
/**
* AES加密
* @param content 待加密的内容
* @param encryptKey 加密密钥
* @return 加密后的byte[]
* @throws Exception
*/
public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));
return cipher.doFinal(content.getBytes("utf-8"));
}
继续阅读与本文标签相同的文章
-
9月书讯:别抱怨读书苦,那是你看世界的路
2026-05-19栏目: 教程
-
首页流量波动大?如何避开猜你喜欢的n个雷区
2026-05-19栏目: 教程
-
Linux基础技术实践#网络安全基础技术实践课程
2026-05-19栏目: 教程
-
AI时代,你的职业会是?99%的人都无法直面!
2026-05-19栏目: 教程
-
centos7 编译安装 openresty
2026-05-19栏目: 教程
