python - How to use CFB mode of AES in pycrypto -
my question how use cfb mode in pycrypto? problem module doesn't accept arbitrary length of iv , key.
>>> crypto.cipher import aes >>> aes = aes.new('123456', aes.mode_cfb, '12345678') traceback (most recent call last): file "<stdin>", line 1, in <module> file "/usr/lib/python2.7/dist-packages/crypto/cipher/aes.py", line 94, in new return aescipher(key, *args, **kwargs) file "/usr/lib/python2.7/dist-packages/crypto/cipher/aes.py", line 59, in __init__ blockalgo.blockalgo.__init__(self, _aes, key, *args, **kwargs) file "/usr/lib/python2.7/dist-packages/crypto/cipher/blockalgo.py", line 141, in __init__ self._cipher = factory.new(key, *args, **kwargs) valueerror: iv must 16 bytes long
next:
>>> aes = aes.new('123456', aes.mode_cfb, '1234567890abcdef') traceback (most recent call last): file "<stdin>", line 1, in <module> file "/usr/lib/python2.7/dist-packages/crypto/cipher/aes.py", line 94, in new return aescipher(key, *args, **kwargs) file "/usr/lib/python2.7/dist-packages/crypto/cipher/aes.py", line 59, in __init__ blockalgo.blockalgo.__init__(self, _aes, key, *args, **kwargs) file "/usr/lib/python2.7/dist-packages/crypto/cipher/blockalgo.py", line 141, in __init__ self._cipher = factory.new(key, *args, **kwargs) valueerror: aes key must either 16, 24, or 32 bytes long
as understanding cfb mode should accept arbitrary iv , key lengths, or wrong?
aes specified key sizes of 128, 192 , 256 bit block size of 128 bit. iv size @ least cbc , cfb mode should equal block size. beyond not part of specification , therefore not interoperable other implementations.
you need use long enough key , iv. if want use password instead of key, use hashing derive one. passwords have lower entropy random keys, need strong (meaning slow) key derivation function transforms given password key. make hard attackers brute-force passwords @ high rate. key derivation function pbkdf2 provided pycrypto. default parameters ok, might want increase iteration count 10,000.
the iv should generated randomly during encryption, doesn't have kept secret. iv prepended ciphertext before sending it. since size of iv known, can sliced off during decryption , used.
Comments
Post a Comment