I'm creating Image encryption and decryption using AES algorithm. All of my code runs well and image is being selected for upload and encryption process runs but following error appears Incorrect AES key length (64 bytes) Screenshot attachedenter image description here
here is my code
#------------------------Encryption -------------------#
def level_one_encrypt(Imagename):
message_image = load_image(Imagename)
size = message_image.size
width, height = size
secret_image = generate_secret(size)
secret_image.save("secret.jpeg")
prepared_image = prepare_message_image(message_image, size)
ciphered_image = generate_ciphered_image(secret_image, prepared_image)
ciphered_image.save("2-share_encrypt.jpeg")
# -------------------- Construct Encrypted Image ----------------#
def construct_enc_image(ciphertext, relength, width, height):
asciicipher = binascii.hexlify(ciphertext)
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
reps = {'a':'1', 'b':'2', 'c':'3', 'd':'4', 'e':'5', 'f':'6', 'g':'7', 'h':'8', 'i':'9', 'j':'10', 'k':'11', 'l':'12', 'm':'13', 'n':'14', 'o':'15', 'p':'16', 'q':'17', 'r':'18', 's':'19', 't':'20', 'u':'21', 'v':'22', 'w':'23', 'x':'24', 'y':'25', 'z':'26'}
asciiciphertxt = replace_all(asciicipher, reps)
step = 3
encimageone=[asciiciphertxt[i:i+step] for i in range(0, len(asciiciphertxt), step)]
# if the last pixel RGB value is less than 3-digits, add a digit as 1
if int(encimageone[len(encimageone)-1]) < 100:
encimageone[len(encimageone)-1] += "1"
if len(encimageone) % 3 != 0:
while len(encimageone) % 3 != 0:
encimageone.append("101")
pixelArray = []
for i in range(len(encimageone)//3):
pixelArray.append((int(encimageone[3*i]),int(encimageone[3*i+1]),int(encimageone[3*i+2])))
image = Image.new("RGB", (width, height))
image.putdata(pixelArray)
return image
#------------------------- Visual-encryption -------------------------#
def encrypt(imagename,password):
plaintext = list()
plaintextstr = ""
im = Image.open(imagename)
pix = im.load()
width = im.size[0]
height = im.size[1]
# break up the image into a list, each with pixel values and then append to a string
for y in range(0,height):
for x in range(0,width):
print (pix[x,y])
plaintext.append(pix[x,y])
print(width)
print(height)
# add 100 to each tuple value to make sure each are 3 digits long.
for i in range(0,len(plaintext)):
for j in range(0,3):
aa = int(plaintext[i][j])+100
plaintextstr = plaintextstr + str(aa)
# length save for encrypted image reconstruction
relength = len(plaintext)
# append dimensions of image for reconstruction after decryption
plaintextstr += "h" + str(height) + "h" + "w" + str(width) + "w"
# make sure that plantextstr length is a multiple of 16 for AES. if not, append "n".
while (len(plaintextstr) % 16 != 0):
plaintextstr = plaintextstr + "n"
# encrypt plaintext
obj = AES.new(password, AES.MODE_CBC, 'This is an IV456')
ciphertext = obj.encrypt(plaintextstr)
# write ciphertext to file for analysis
cipher_name = imagename + ".crypt"
g = open(cipher_name, 'w')
g.write(ciphertext)
construct_enc_image(ciphertext,relength,width,height)
print("Visual Encryption done.......")
level_one_encrypt("visual_encrypt.jpeg")
print("2-Share Encryption done.......")
# ---------------------- decryption ---------------------- #
def decrypt(ciphername,password):
secret_image = Image.open("secret.jpeg")
ima = Image.open("2-share_encrypt.jpeg")
new_image = generate_image_back(secret_image, ima)
new_image.save("2-share_decrypt.jpeg")
print("2-share Decryption done....")
cipher = open(ciphername,'r')
ciphertext = cipher.read()
# decrypt ciphertext with password
obj2 = AES.new(password, AES.MODE_CBC, 'This is an IV456')
decrypted = obj2.decrypt(ciphertext)
# parse the decrypted text back into integer string
decrypted = decrypted.replace("n","")
# extract dimensions of images
newwidth = decrypted.split("w")[1]
newheight = decrypted.split("h")[1]
# replace height and width with emptyspace in decrypted plaintext
heightr = "h" + str(newheight) + "h"
widthr = "w" + str(newwidth) + "w"
decrypted = decrypted.replace(heightr,"")
decrypted = decrypted.replace(widthr,"")
# reconstruct the list of RGB tuples from the decrypted plaintext
step = 3
finaltextone=[decrypted[i:i+step] for i in range(0, len(decrypted), step)]
finaltexttwo=[(int(finaltextone[int(i)])-100,int(finaltextone[int(i+1)])-100,int(finaltextone[int(i+2)])-100) for i in range(0, len(finaltextone), step)]
# reconstruct image from list of pixel RGB tuples
newim = Image.new("RGB", (int(newwidth), int(newheight)))
newim.putdata(finaltexttwo)
newim.save("visual_decrypt.jpeg")
print("Visual Decryption done......")
# ---------------------
# GUI stuff starts here
# ---------------------
def pass_alert():
messagebox.showinfo("Password Alert","Please enter a password.")
def enc_success(imagename):
messagebox.showinfo("Success","Encrypted Image: " + imagename)
# image encrypt button event
def image_open():
global file_path_e
enc_pass = passg.get()
if enc_pass == "":
pass_alert()
else:
password = hashlib.sha256(enc_pass.encode('utf-8')).hexdigest()
filename = filedialog.askopenfilename()
file_path_e = os.path.dirname(filename)
encrypt(filename,password)
# image decrypt button event
def cipher_open():
global file_path_d
dec_pass = passg.get()
if dec_pass == "":
pass_alert()
else:
password = hashlib.sha256(dec_pass.encode('utf-8')).hexdigest()
filename = filedialog.askopenfilename()
file_path_d = os.path.dirname(filename)
decrypt(filename,password)
class App:
def __init__(self, master):
global passg
title = "Image Encryption"
author = "Made by Aqsa & Sabooha"
msgtitle = Message(master, text =title)
msgtitle.config(font=('helvetica', 17, 'bold'), width=200)
msgauthor = Message(master, text=author)
msgauthor.config(font=('helvetica',10), width=200)
canvas_width = 200
canvas_height = 50
w = Canvas(master,
width=canvas_width,
height=canvas_height)
msgtitle.pack()
msgauthor.pack()
w.pack()
passlabel = Label(master, text="Enter Encrypt/Decrypt Password:")
passlabel.pack()
passg = Entry(master, show="*", width=20)
passg.pack()
self.encrypt = Button(master,
text="Encrypt", fg="black",
command=image_open, width=25,height=5)
self.encrypt.pack(side=LEFT)
self.decrypt = Button(master,
text="Decrypt", fg="black",
command=cipher_open, width=25,height=5)
self.decrypt.pack(side=RIGHT)
any solution will be highly appreciated.