Skip to content Skip to sidebar Skip to footer

Valueerror: Data Must Be Aligned To Block Boundary In Ecb Mode

I am trying aes 128 encryption in ECB mode with the following code. from Crypto.Cipher import AES key = 'abcdefghijklmnop' cipher = AES.new(key.encode('utf8'), AES.MODE_ECB) msg =

Solution 1:

For padding and un-padding, you may use inbuilt functions of Crypto library, below is a working solution to your problem.

from Crypto.Util.Padding import pad, unpad
from Crypto.Cipher import AES
BLOCK_SIZE = 32 # Bytes

key = 'abcdefghijklmnop'
cipher = AES.new(key.encode('utf8'), AES.MODE_ECB)
msg = cipher.encrypt(pad(b'hello', BLOCK_SIZE))
print(msg.hex())
decipher = AES.new(key.encode('utf8'), AES.MODE_ECB)
msg_dec = decipher.decrypt(msg)
print(unpad(msg_dec, BLOCK_SIZE))

Post a Comment for "Valueerror: Data Must Be Aligned To Block Boundary In Ecb Mode"