9

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 = cipher.encrypt(b'hello')
print(msg.hex())
decipher = AES.new(key.encode('utf8'), AES.MODE_ECB)
msg_dec = decipher.decrypt(msg)
print(msg_dec)

but I'm getting "ValueError: Data must be aligned to block boundary in ECB mode". It works fine if string is a multiple of 16. I don't know how to do padding, unpadding. How can we solve this ? Please help

Jozf
  • 161
  • 2
  • 4
  • 9
  • You can solve this by applying a padding (add some whitespace or `\0` characters at the end). This is required since ECB is a block cipher. – Pieter De Clercq Sep 05 '18 at 09:10
  • Possible duplicate of [How to add a padding to the data to make it acceptable for AES256 encryption algorithm in pycrypto library](https://stackoverflow.com/questions/2108047/how-to-add-a-padding-to-the-data-to-make-it-acceptable-for-aes256-encryption-alg) – Klaus D. Sep 05 '18 at 09:11
  • [implementing-aes-ecb-pkcs5-padding-in-python](https://stackoverflow.com/questions/64203881/implementing-aes-ecb-pkcs5-padding-in-python), [aes-cbc-pkcs5padding-from-java-to-python](https://stackoverflow.com/questions/73442479/aes-cbc-pkcs5padding-from-java-to-python), [aes-cbc-128-192-and-256-encryption-decryption-in-python-3-using-pkcs7-padding](https://stackoverflow.com/questions/39653074/aes-cbc-128-192-and-256-encryption-decryption-in-python-3-using-pkcs7-padding) – Nick Dong Jul 12 '23 at 03:50

2 Answers2

12

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))
Abhishake Gupta
  • 2,939
  • 1
  • 25
  • 34
  • 1
    Why is `32` used as `BLOCK_SIZE`? Should it not be `16` because the AES block size is 128 bits (16 bytes)? – Patrick Jun 12 '22 at 11:36
1

improving on @Abhishake Gupta's answer using pycryptodomex pip install pycryptodomex

import base64
from Cryptodome.Cipher import AES
from Cryptodome.Util.Padding import pad, unpad
###################
class Cryptor:
    def __init__(self, key):
      self.SECRET_KEY = str(key).encode("utf-8")
      self.BLOCK_SIZE = 32 # Bytes
      self.CIPHER = AES.new(self.SECRET_KEY, AES.MODE_ECB) # never use ECB in strong systems obviously

    def encrypt(self, text):
       text = str(text).encode("utf-8")
       return base64.b64encode(self.CIPHER.encrypt(pad(text, self.BLOCK_SIZE))).decode("utf-8")
    
    def decrypt(self, encoded_text):
       self.CIPHER = AES.new(self.SECRET_KEY, AES.MODE_ECB)
       return unpad(self.CIPHER.decrypt(base64.b64decode(encoded_text)), self.BLOCK_SIZE).decode("utf-8")
       
cryptor = Cryptor("1234567890123456")
text = "hello world"
text = cryptor.encrypt(text)
print(text)
print(cryptor.decrypt(text))
Walulya francis
  • 353
  • 2
  • 10