0

The error The code

# Import necessary modules from the cryptography library
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
import os

# Generate a random 16-byte key using the os.urandom() function
key = os.urandom(16)

# Create an AES cipher object using the Cipher class from the cryptography library
aesCipher = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend())

# Create an encryption object and a decryption object using the encryptor() and decryptor()                     
aesEncryptor = aesCipher.encryptor()
aesDecryptor = aesCipher.decryptor()

# Define some data to encrypt
data_to_encrypt = "Helloworld".encode('utf-8')

# Encrypt the data using the AES encryption object
encrypted_data = aesEncryptor.update(data_to_encrypt) + aesEncryptor.finalize()

# Decrypt the encrypted data using the AES decryption object
decrypted_data = aesDecryptor.update(encrypted_data) + aesDecryptor.finalize()

# Print the original data and the decrypted data to verify that they match
print("Original data:", data_to_encrypt)
print("Decrypted data:", decrypted_data)

I am getting a value error in the above code.Do suggest your thoughts on how to troubleshoot the above

  • Hi, please read [Why should I not upload images of code/data/errors?](https://meta.stackoverflow.com/a/285557/7353417) – pierpy May 06 '23 at 10:32

1 Answers1

0

you are using encryption algorithm that encrypts data block by block. it means that you need to validate that your blocks of data are multiply of block size for example if your block size is 256 bytes, you need to validate that length the length of the cipher / 256 = 0 so what you need to do is to pad your last block to be as block size. the appropriate way is to concatenation zero's to the last block. for more info read some about padding:

https://cryptography.io/en/latest/hazmat/primitives/padding/

and for implementation, you can use this answer:

Encrypt and decrypt using PyCrypto AES-256

Dump Eldor
  • 92
  • 1
  • 11