python - IV must be 16 bytes long error in AES encryption -
i using pycrypto module aes encryption. , using documentation have write down below function al;ways gives error iv must 16 bytes long
using 16 byte long iv.
def aes_encrypt(plaintext): """ """ key = **my key comes here** iv = binascii.hexlify(os.urandom(16)) # used without binascii.hexlify) aes_mode = aes.mode_cbc obj = aes.new(key, aes_mode, iv) ciphertext = obj.encrypt(plaintext) return ciphertext
use this:
from crypto.cipher import aes import binascii,os def aes_encrypt(plaintext): key = "00112233445566778899aabbccddeeff" iv = os.urandom(16) aes_mode = aes.mode_cbc obj = aes.new(key, aes_mode, iv) ciphertext = obj.encrypt(plaintext) return ciphertext
works below:
>>> aes_encrypt("testtesttesttest") 'r_\x18\xaa\xac\x9c\xdb\x18n\xc1\xa4\x98\xa6sm\xd3' >>>
that's difference:
>>> iv = binascii.hexlify(os.urandom(16)) >>> iv '9eae3db51f96e53f94dff9c699e9e849' >>> len(iv) 32 >>> iv = os.urandom(16) >>> iv '\x16fdw\x9c\xe54]\xc2\x12!\x95\xd7zf\t' >>> len(iv) 16 >>>
Comments
Post a Comment