0

Please i'm trying to encrypt an image using the ECB, CBC and CFB modes. when I try to execute the program, i get the error "TypeError: IV is not meaningful for the ECB mode". Here is my code: I will really appreciate any help as am new to python. Thanks so much

from PIL import Image
#from PIL.Image import core as image
import os
import sys
from Crypto.Cipher import AES

Block_size=16
IV_size=16
def encrypt_file(input_file,output_file,cipher_mode):
          input_img=Image.open(input_file)
          key="770A8A65DA156D24EE2A093277530142"
          if cipher_mode=='ECB':
               mode=AES.MODE_ECB
          elif cipher_mode=='CBC':
               mode=AES.MODE_CBC
          else:
              mode=AES.MODE_CFB
          i=os.urandom(IV_size)
          aes=AES.new(key,mode,i)
          img_str=input_img.tostring()
          #Pad the image string to the input block size
          img_pad_lenght=Block_size-len(img_str)/Block_size

          img_str+=img_pad_lenght*"~"
          #generate the encrypted image string
          encrypted_img_str=aes.encrypt(img_str)

          #create an image from the encrypted string
          encrypted_img=Image.frombuffer('RGB',input_img.size, encrypted_img_str,'raw','RGB',0,1)
          #create and save the output image
          encrypted_img.save(output_file,'PNG')
          print("Encrypted using AES in " + cipher_mode + " mode and saved to \"" + output_file")

encrypt_file('Linux-icon.png','output_file.png','ECB')

Amaka
  • 13
  • 4

0 Answers0