Imgecode
Imgecode
/usr/bin/env python
# ----------------- Header Files ---------------------#
import sys
import random
import argparse
import logging
from tkinter import * # Changed from Tkinter to tkinter for Python 3
from tkinter import filedialog # Changed from tkFileDialog to filedialog for
Python 3
from tkinter import messagebox # Changed from tkMessageBox to messagebox for
Python 3
import os
from PIL import Image
import math
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
from tkinter import Tk, Button, LEFT, RIGHT, Entry, Label, Message, Canvas
import hashlib
import binascii
import numpy as np
global password
def load_image(name):
return Image.open(name)
return new_secret_image
return ciphered_image
return new_image
#------------------------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")
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 += str(aa).encode()
# encrypt plaintext
obj = AES.new(password, AES.MODE_CBC, b'This is an IV456')
ciphertext = obj.encrypt(plaintextstr)
try:
# Open the encrypted file in binary mode
with open(ciphername, 'rb') as cipher_file:
ciphertext = cipher_file.read()
except Exception as e:
print("Error during decryption:", e)
# GUI stuff
def pass_alert():
messagebox.showinfo("Password Alert", "Please enter a password.")
def image_open():
global file_path_e
enc_pass = passg.get()
if enc_pass == "":
pass_alert()
else:
password = hashlib.sha256(enc_pass.encode()).digest()
filename = filedialog.askopenfilename()
file_path_e = os.path.dirname(filename)
encrypt(filename, password)
def cipher_open():
global file_path_d
dec_pass = passg.get()
if dec_pass == "":
pass_alert()
else:
password = hashlib.sha256(dec_pass.encode()).digest()
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 Aditya"
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()
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)
# Main function
if __name__ == "__main__":
root = Tk()
root.wm_title("Image Encryption")
app = App(root)
root.mainloop()
name__ == "__main__":