Python AES实现的区别

mar*_*llm 5 python encryption cryptography encryption-symmetric pycrypto

我正在比较pycryptocryptography.io库中的Python实现.

from cryptography.hazmat.primitives.ciphers import Cipher, modes, algorithms
from cryptography.hazmat.backends import default_backend  # http://cryptography.io
from Crypto.Cipher import AES  # http://pycrypto.org

key = b'Sixteen byte key'
iv = b'Sixteen byte ivv'
cipher1 = AES.new(key, AES.MODE_CFB, iv)
cipher2 = Cipher(algorithms.AES(key), modes.CFB(iv), default_backend())

plaintext = b"Plaintext"

print(cipher1.encrypt(plaintext))
print(cipher1.decrypt(plaintext))
print(cipher2.encryptor().update(plaintext))
print(cipher2.decryptor().update(plaintext))
Run Code Online (Sandbox Code Playgroud)

MWE打印:

b'\xe4\xb4\xeb\xe3Si\x9ap\xee'
b'7\xda\x98\xee\x05\xe4\xa0\xc7,'
b'\xe4"\xd4mo\xa3;\xa9\xe0'
b'\xe4"\xd4mo\xa3;\xa9\xe0'
Run Code Online (Sandbox Code Playgroud)

为什么输出不同?

Ale*_*nor 4

答案似乎是 PyCrypto 默认实现 CFB-8,这是普通 CFB 的变体。 https://github.com/digitalbazaar/forge/issues/100更详细地描述了该问题。