我有以下python代码:
def AES_build_cipher(key, iv, op):
return EVP.Cipher(alg='aes_128_cbc', key=key, iv=iv, op=op, padding=True) # PKCS#5 paddig
def AES_encrypt(key, msg, iv): # key, iv -> bytes, msg -> text
if iv is None:
raise ValueError("IV must be defined!")
# Return the encryption function
def encrypt(data):
cipher = AES_build_cipher(key, iv, ENCRYPTION)
v = cipher.update(data)
v = v + cipher.final()
del cipher
return v
return encrypt(msg)
它工作正常(通过 M2Crypto 加密/解密)。
用于解密的Java代码:
public static String AESDecrypt(String b64data, byte[] key, byte[] iv) throws CipherException {
try {
aesCipher_ = Cipher.getInstance("AES/CBC/PKCS5Padding");
aesCipher_.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
final byte[] byteData = Base64.decode(b64data, Base64.DEFAULT);
final byte[] decryptedData = aesCipher_.doFinal(byteData);
return new String(decryptedData);
} catch (Exception e) {
throw new CipherException(e);
}
}
数据:
- iv = 8b9123ba6712612fb98452aac3854838(十六进制表示)
- 文本 = 12345678901234567890(简单文本)
- 密文 = af87d97bf9779efcff0386d4eaee18619dc8f1fe7c5adea2a91657f53491bc2(十六进制表示)
- 密码 = 791a06ee369dc2f842c655f6bec8ce2(十六进制表示)
结果:
- Exp:'12345678901234567890'
- 得到:'1����$V��c�J�}7890'
IV 看起来有问题(结果的前 16 个字节)。但我不知道我错过了什么。