这是我的Python代码,我可以使用M2Crypto成功地加密和解密:from base64 import b64encode, b64decode
import M2Crypto
class AESEncryptionService(object):
def encrypt(self, key, msg):
return self.__cipher(key, msg, 1)
def decrypt(self, key, msg):
try:
decrypted = self.__cipher(key, msg, 0)
return decrypted
except:
return False
def __cipher(self, key, msg, op):
iv = '\0' * 16
iv.encode('ascii')
cipher = M2Crypto.EVP.Cipher(alg='aes_128_cbc', key=key, iv=iv, op=op)
v = cipher.update(msg)
v = v + cipher.final()
del cipher
return v
enc_service = AESEncryptionService()
cipher = b64encode(enc_service.encrypt("FD496E240E7822552BC0D63C03AB7ABB", "Hello Hello Hello Hello Hello Hello Hello"))
print cipher
plaintext = enc_service.decrypt("FD496E240E7822552BC0D63C03AB7ABB", b64decode(cipher))
print plaintext
输出:
^{pr2}$
我用PHP编写了一个简单的测试脚本,希望得到相同的结果:<?php
$key = 'FD496E240E7822552BC0D63C03AB7ABB';
$plain = 'Hello Hello Hello Hello Hello Hello Hello';
$iv = '0000000000000000';
$cipher = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plain, MCRYPT_MODE_CBC, $iv);
$plain = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $cipher, MCRYPT_MODE_CBC, $iv);
echo base64_encode($cipher), '
';
echo $plain;
输出:v5QinnNaEkn8myZa31Ikl5DrrtSXrhebDERJz4uqUgWMvbZ3datyCbm9WHPGlAo7
Hello Hello Hello Hello Hello Hello Hello
你能帮我修改Python代码,使其输出与PHP相同吗?我知道PHP的AES实现是正确的,因为我可以在.NET中解密它。无法在.NET中解密M2Crypto Python库输出的字符串。在
我想可能和填充物有关。在
平方米Crypto.EVP.密码具有填充参数。我试着把它设为1,2,3,128,但它似乎不影响密码。在