PyCryptodome AES CBC 加密没有提供所需的输出
PyCryptodome AES CBC encryption does not give desired output
我正在尝试使用 Pycryptodome (3.7.0)
在 Python (2.7.14) 中使用 CBC 模式加密和解密 AES 中的简单文本
这是我尝试加密的代码:
from Crypto.Cipher import AES
from Crypto.Util import Padding
import base64
encryption_key = "1111111111111111111111111111111111111111111111111111111111111111".decode("hex")
text = "Test text"
text_padded = Padding.pad(text, AES.block_size)
iv = "0000000000000000"
cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
cipher_enc = cipher.encrypt(text_padded)
encrypted = iv + cipher_enc
print encrypted
print base64.b64encode(encrypted)
print encrypted.encode("hex")
print base64.b64encode(encrypted).encode("hex")
输出是
0000000000000000X???]????H?
MDAwMDAwMDAwMDAwMDAwMFje9RzRXc3LHt8GBBLTSPQ=
3030303030303030303030303030303058def51cd15dcdcb1edf060412d348f4
4d4441774d4441774d4441774d4441774d4441774d466a6539527a525863334c4874384742424c545350513d
但是当我向 http://aes.online-domain-tools.com/ 输入相同的键、文本和初始向量值时,我得到了不同的结果。
输出是:6a56bc5c0b05892ae4e63d0ca6b3169b
截图如下:
我做错了什么?如何通过pycrypto获取在线加密网站的输出值?
首先在 python 3 中:python 3 对字节和字符串要严格得多。
这重现了给定的示例:
from Crypto.Cipher import AES
encryption_key = 32 * b'\x11'
text = "Test text".encode()
text_padded = text + (AES.block_size - (len(text) % AES.block_size)) * b'\x00'
iv = 16 * b'\x00'
cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
cipher_enc = cipher.encrypt(text_padded)
print(encryption_key.hex())
print(iv.hex())
print(cipher_enc.hex())
# 1111111111111111111111111111111111111111111111111111111111111111
# 00000000000000000000000000000000
# 6a56bc5c0b05892ae4e63d0ca6b3169b
注意不需要encrypted = iv + cipher_enc
;您已经在 CBC 模式下 运行ning AES。
也在 python 2 上达到了 运行:
from Crypto.Cipher import AES
encryption_key = 32 * b'\x11'
text = "Test text".encode()
text_padded = text + (AES.block_size - (len(text) % AES.block_size)) * b'\x00'
iv = 16 * b'\x00'
cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
cipher_enc = cipher.encrypt(text_padded)
print(encryption_key.encode('hex'))
print(iv.encode('hex'))
print(cipher_enc.encode('hex'))
我正在尝试使用 Pycryptodome (3.7.0)
在 Python (2.7.14) 中使用 CBC 模式加密和解密 AES 中的简单文本这是我尝试加密的代码:
from Crypto.Cipher import AES
from Crypto.Util import Padding
import base64
encryption_key = "1111111111111111111111111111111111111111111111111111111111111111".decode("hex")
text = "Test text"
text_padded = Padding.pad(text, AES.block_size)
iv = "0000000000000000"
cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
cipher_enc = cipher.encrypt(text_padded)
encrypted = iv + cipher_enc
print encrypted
print base64.b64encode(encrypted)
print encrypted.encode("hex")
print base64.b64encode(encrypted).encode("hex")
输出是
0000000000000000X???]????H?
MDAwMDAwMDAwMDAwMDAwMFje9RzRXc3LHt8GBBLTSPQ=
3030303030303030303030303030303058def51cd15dcdcb1edf060412d348f4
4d4441774d4441774d4441774d4441774d4441774d466a6539527a525863334c4874384742424c545350513d
但是当我向 http://aes.online-domain-tools.com/ 输入相同的键、文本和初始向量值时,我得到了不同的结果。
输出是:6a56bc5c0b05892ae4e63d0ca6b3169b
截图如下:
我做错了什么?如何通过pycrypto获取在线加密网站的输出值?
首先在 python 3 中:python 3 对字节和字符串要严格得多。
这重现了给定的示例:
from Crypto.Cipher import AES
encryption_key = 32 * b'\x11'
text = "Test text".encode()
text_padded = text + (AES.block_size - (len(text) % AES.block_size)) * b'\x00'
iv = 16 * b'\x00'
cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
cipher_enc = cipher.encrypt(text_padded)
print(encryption_key.hex())
print(iv.hex())
print(cipher_enc.hex())
# 1111111111111111111111111111111111111111111111111111111111111111
# 00000000000000000000000000000000
# 6a56bc5c0b05892ae4e63d0ca6b3169b
注意不需要encrypted = iv + cipher_enc
;您已经在 CBC 模式下 运行ning AES。
也在 python 2 上达到了 运行:
from Crypto.Cipher import AES
encryption_key = 32 * b'\x11'
text = "Test text".encode()
text_padded = text + (AES.block_size - (len(text) % AES.block_size)) * b'\x00'
iv = 16 * b'\x00'
cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
cipher_enc = cipher.encrypt(text_padded)
print(encryption_key.encode('hex'))
print(iv.encode('hex'))
print(cipher_enc.encode('hex'))