标签:ram span byte 内容 字节 就是 key ret throws
AES加密解密过程中,由于是在jetty服务下开发的,运行中文不乱码,但是在测试在tomcat下还是出现了中文乱码(已经在server.xml配过了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"));
}
标签:ram span byte 内容 字节 就是 key ret throws
原文地址:https://www.cnblogs.com/rita1993/p/9933321.html