2
我有以下Python代碼:使用Python M2Crypto AES加密與Java
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(十六進制表示法)
- 文本= 123456789(簡單的文本)
- 密文= af87d97bf9779efcff0386d4eaee18619dc8f1fe7c5adea2a91657f53491bc2(十六進制 表示)
- 密碼= 791a06ee369dc2f842c655f6bec8ce2(十六進制表示法)
結果:
- 精通: '123456789'
- GOT: '$ 1 VCJ} 7890'
看起來像IV(結果的前16個字節)有問題。但我不知道我錯過了什麼。