Differences in Python AES Implementation

I am comparing AES implementations in Python from pycrypto and cryptography.io .

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))

MWE Printing:

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'

Why different outputs?

+4
source share
2 answers

The answer is that PyCrypto implements CFB-8 by default, which is an option for regular CFB. https://github.com/digitalbazaar/forge/issues/100 describes the problem in more detail.

+4
source

Based on Alex Gaynor's answer, here is a more detailed description:

. 16 (128 ) . PyCrypto 8 , :

cipher1 = AES.new(key, AES.MODE_CFB, iv, segment_size=128)

.

+2

Source: https://habr.com/ru/post/1540507/


All Articles