不管是 python 加密 -> java 解密,或 java加密->python解密 总是有前几个字节(3-8个不定)解不出来,难道是我操作方式不对?
python 加密代码:
python
def encrypt(content):
iv_time = long(time.time() * 1000)
iv = struct.pack('Q', iv_time)
des = DES3.new(http_api_app_secret, DES3.MODE_CBC, iv)
content = encrypt_pad(content)
content = des.encrypt(content)
content = urllib.quote(base64.b64encode(content))
return str(iv_time) + content
java 解密代码:
`````
byte[] decrypt(byte[] key, byte[] iv, byte[] message) {
byte[] result = null;
try {
Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
SecretKey secretKey = skf.generateSecret(new DESedeKeySpec(key));
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
result = cipher.doFinal(message);
} catch (Exception e) {
throw new SecurityException(e);
}
return result;
}
两种语言自己加密解密都是正常的
1
hao123yinlong 2015-03-04 13:49:09 +08:00 1
看看是否是字符编码造成
|
2
john990 OP @hao123yinlong 两边都是UTF-8编码,而且除了前几个字节其他部分都能正常解密
|
3
juntao 2015-03-04 15:17:14 +08:00 1
两边的IV一样吗?我遇到过IV不一致的情况会有这种表现。
|