情况就是:通过3DS加密后的中文字符串,
解密:在windows默认字符集的环境下是不会出现乱码,但是在linux环境下就可能会出现中文乱码
3DES解密方法:
public static String decryptThreeDESECB(final String src, final String key) throws Exception { // --通过base64解码,将字符串转成byte数组 final BASE64Decoder decoder = new BASE64Decoder(); final byte[] bytesrc = decoder.decodeBuffer(src); // --解密的key DESedeKeySpec dks = null; if(StringUtils.isEmpty(key)){ dks = new DESedeKeySpec(AppSecret.getBytes("UTF-8")); }else{ dks= new DESedeKeySpec(key.getBytes("UTF-8")); } final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); final SecretKey securekey = keyFactory.generateSecret(dks); // --Chipher对象解密` final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); //解密向量向量 final IvParameterSpec iv = new IvParameterSpec(AppKey.getBytes("UTF-8")); cipher.init(Cipher.DECRYPT_MODE, securekey,iv); final byte[] retByte = cipher.doFinal(bytesrc); //return new String(retByte); return new String(retByte,"UTF-8"); }
上面代码最后返回结果处 如果 不对new String(retByte) 进行指定字符集 就有可能导致在linux环境下出现乱码
原因你加密过程使用的字符集要和你解密时候用的字符集保持一致,如果不对结果进行指定字符集:会默认使用系统字符集,如果和你加密过程不一致极容易出现中文乱码,
也许讲的不是很对,但是完美解决了我项目中的问题 !
这是个人第一次的博客随笔,意在积累自己工作中的点滴, 更希望能帮到各位朋友解决遇到相同的问题。
如果有什么不对的欢迎留言!
希望能帮到各位!