Python Rijndael 加密
Python Rijndael Encryption
我正在尝试在 Python3 中模仿 http://www.hanewin.net/encrypt/aes/aes-test.htm 的 Rijndael (AES) 加密。
具体来说,当我使用以下输入时:
Key in hex = AAAABBBBCCCCDDDDEEEEFFFF00001111
Plaintext in hex = 11112222333344445555666677778888
我希望输出为:
Ciphertext in hex = ec151e51fc722c06073928d17fa4f6da
从网页源代码看,是用的ECB。我已经安装了 PyCrypto 并按照示例进行了加密,这就是我得到的:
>>> from Crypto.Cipher import AES
>>>
>>> KEY = 'AAAABBBBCCCCDDDDEEEEFFFF00001111'
>>> rijn = AES.new(KEY, AES.MODE_ECB)
>>> plaintext = '11112222333344445555666677778888'
>>> ciphertext = rijn.encrypt(plaintext)
>>> ciphertext
b'\xf8Gy\x17\x85$\xf4?\xd3}\x91\xa1\xad\xab\xa1\x07g\xf7P\xd4:\x7f\xc0\x18m)\rTu,\xd0\xa2'
所以我的密文是那个长二进制字符串(如果这是正确的术语)。假设所有设置都正确,它应该相当于 ec151e51fc722c06073928d17fa4f6da
。如何将 b'\xf8Gy\x17\x85$\xf4?\xd3}\x91\xa1\xad\xab\xa1\x07g\xf7P\xd4:\x7f\xc0\x18m)\rTu,\xd0\xa2'
转换为 ec151e51fc722c06073928d17fa4f6da
?
更新:
根据 Jon 在评论中的建议,我导入了 binascci,现在我得到以下输出:
>>> import binascii
>>>
>>> binascii.hexlify(ciphertext)
b'f84779178524f43fd37d91a1adaba10767f750d43a7fc0186d290d54752cd0a2'
所以可能仍然是我的密码正确,但转换时有些不同。以下是网页源代码的一些摘录:
theForm.ciphertext.value = byteArrayToHex(rijndaelEncrypt(pt, key, "ECB"));
// This function takes an array of bytes (byteArray) and converts them
// to a hexadecimal string. Array element 0 is found at the beginning of
// the resulting string, high nibble first. Consecutive elements follow
// similarly, for example [16, 255] --> "10ff". The function returns a
// string.
function byteArrayToHex(byteArray) {
var result = "";
if (!byteArray)
return;
for (var i=0; i<byteArray.length; i++)
result += ((byteArray[i]<16) ? "0" : "") + byteArray[i].toString(16);
return result;
}
正如上面分享的,这是评论者帮助得出的答案:
>>> from Crypto.Cipher import AES
>>> import binascii
>>> KEY = binascii.unhexlify('AAAABBBBCCCCDDDDEEEEFFFF00001111')
>>> plaintext = binascii.unhexlify('11112222333344445555666677778888')
>>> rijn = AES.new(KEY, AES.MODE_ECB)
>>> ciphertext = rijn.encrypt(plaintext)
>>> binascii.hexlify(ciphertext)
b'ec151e51fc722c06073928d17fa4f6da'
>>> print(binascii.hexlify(ciphertext).decode('utf-8'))
ec151e51fc722c06073928d17fa4f6da
我正在尝试在 Python3 中模仿 http://www.hanewin.net/encrypt/aes/aes-test.htm 的 Rijndael (AES) 加密。
具体来说,当我使用以下输入时:
Key in hex = AAAABBBBCCCCDDDDEEEEFFFF00001111
Plaintext in hex = 11112222333344445555666677778888
我希望输出为:
Ciphertext in hex = ec151e51fc722c06073928d17fa4f6da
从网页源代码看,是用的ECB。我已经安装了 PyCrypto 并按照示例进行了加密,这就是我得到的:
>>> from Crypto.Cipher import AES
>>>
>>> KEY = 'AAAABBBBCCCCDDDDEEEEFFFF00001111'
>>> rijn = AES.new(KEY, AES.MODE_ECB)
>>> plaintext = '11112222333344445555666677778888'
>>> ciphertext = rijn.encrypt(plaintext)
>>> ciphertext
b'\xf8Gy\x17\x85$\xf4?\xd3}\x91\xa1\xad\xab\xa1\x07g\xf7P\xd4:\x7f\xc0\x18m)\rTu,\xd0\xa2'
所以我的密文是那个长二进制字符串(如果这是正确的术语)。假设所有设置都正确,它应该相当于 ec151e51fc722c06073928d17fa4f6da
。如何将 b'\xf8Gy\x17\x85$\xf4?\xd3}\x91\xa1\xad\xab\xa1\x07g\xf7P\xd4:\x7f\xc0\x18m)\rTu,\xd0\xa2'
转换为 ec151e51fc722c06073928d17fa4f6da
?
更新: 根据 Jon 在评论中的建议,我导入了 binascci,现在我得到以下输出:
>>> import binascii
>>>
>>> binascii.hexlify(ciphertext)
b'f84779178524f43fd37d91a1adaba10767f750d43a7fc0186d290d54752cd0a2'
所以可能仍然是我的密码正确,但转换时有些不同。以下是网页源代码的一些摘录:
theForm.ciphertext.value = byteArrayToHex(rijndaelEncrypt(pt, key, "ECB"));
// This function takes an array of bytes (byteArray) and converts them
// to a hexadecimal string. Array element 0 is found at the beginning of
// the resulting string, high nibble first. Consecutive elements follow
// similarly, for example [16, 255] --> "10ff". The function returns a
// string.
function byteArrayToHex(byteArray) {
var result = "";
if (!byteArray)
return;
for (var i=0; i<byteArray.length; i++)
result += ((byteArray[i]<16) ? "0" : "") + byteArray[i].toString(16);
return result;
}
正如上面分享的,这是评论者帮助得出的答案:
>>> from Crypto.Cipher import AES
>>> import binascii
>>> KEY = binascii.unhexlify('AAAABBBBCCCCDDDDEEEEFFFF00001111')
>>> plaintext = binascii.unhexlify('11112222333344445555666677778888')
>>> rijn = AES.new(KEY, AES.MODE_ECB)
>>> ciphertext = rijn.encrypt(plaintext)
>>> binascii.hexlify(ciphertext)
b'ec151e51fc722c06073928d17fa4f6da'
>>> print(binascii.hexlify(ciphertext).decode('utf-8'))
ec151e51fc722c06073928d17fa4f6da