Exception has occurred: ValueError Data must be aligned to block boundary in ECB mode

333 views Asked by At

The code below throws an error "Exception has occurred: ValueError Data must be aligned to block boundary in ECB mode" when ran. This is an encrypted json file and no matter what I do it throws this error. Any ideas?

Im really just trying to make or find something that can take encrypted data from a file and decrypt it. The only thing that worked was this site. It worked perfectly in aes ecb but only allows files up to 2mb.

from Crypto.Cipher import AES

encFile = open('file.json', 'r', encoding='Latin1')
encFile = encFile.read()
key = open('Key.txt', 'r')
key = key.read()
cipher = AES.new(key.encode("utf8"), AES.MODE_ECB)

encFile = encFile.encode("utf8")
decrypted = cipher.decrypt(encFile)
1

There are 1 answers

0
Aemyl On

AES is a block cipher that operates on 128-bit blocks, that's 16 bytes. In ECB mode, the message is split into blocks of that size and each block is encrypted separately. if the length of your message is not a multiple of the block size, there is an incomplete block at the end that can't be encrypted (at least not with ECB). You have two options:

  1. add padding to the data you want to encrypt
  2. use a different mode of operation that doesn't require the message length to be a multiple of 16 bytes.

last but not least, it should be noted that ECB is insecure because identical plain text blocks will be encrypted to identical cipher text blocks. It looks like you're using pycryptodome, which provides authenticated modes such as GCM. If you want to detect modifications, you should use an authenticated mode.