这个想法是AES加密一个字符串并将其发送到我的服务器端,然后在那里解密它以获得真正的字符串。但它没有用,奇怪的是我发现结果是不同的,即使你的AES-256分别加密python和php上的字符串。
Python示例:
from Crypto.Cipher import AES
import base64
original = "originalstring"
key = "12345678123456781234567812345678"
IV = "1234567812345678"
b64encodedOnly = base64.b64encode(original)
encryptCipher = AES.new(key, AES.MODE_CFB, IV)
encrypted = base64.b64encode(encryptCipher.encrypt(original))
decryptCipher = AES.new(key, AES.MODE_CFB, IV)
decrypted = decryptCipher.decrypt(base64.b64decode(encrypted))
print "original:", original
print "b64encoded only:", b64encodedOnly
print "AES-256 encrypted:", encrypted
print "AES-256 decrypted:", decrypted
Python输出:
original: originalstring
b64encoded only: b3JpZ2luYWxzdHJpbmc=
AES-256 encrypted: Rl4MMQojmBm0e6iuCl8=
AES-256 decrypted: originalstring
PHP示例:
<?php
$original = "originalstring";
$key = "12345678123456781234567812345678";
$IV = "1234567812345678";
$b64encodedOnly = base64_encode($original);
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $original, MCRYPT_MODE_CFB, $IV));
$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($encrypted), MCRYPT_MODE_CFB, $IV);
echo "original: " . $original . "<br />";
echo "b64encoded only: " . $b64encodedOnly . "<br />";
echo "AES-256 encrypted: " . $encrypted . "<br />";
echo "AES-256 decrypted: " . $decrypted . "<br />";
?>
PHP输出:
original: originalstring
b64encoded only: b3JpZ2luYWxzdHJpbmc=
AES-256 encrypted: +w9F6zLD2avTt9fj8aM=
AES-256 decrypted: originalstring
正如您在AES-256(CFB模式)加密后所看到的那样,基本64编码,"originalstring"
在python上变为"Rl4MMQojmBm0e6iuCl8="
而在PHP上变为"+w9F6zLD2avTt9fj8aM="
。显然base64的功能相同,因为编码原始字符串的结果是相同的,所以唯一的问题似乎是在AES加密方面,我相信它是在不同平台上实现的相同算法,结果怎么会不同?
修改
我发现了问题: MCRYPT_RIJNDAEL_256不等同于AES_256。