0% found this document useful (0 votes)
10 views

Python Coding for Final Year Project

Uploaded by

davidtellez12185
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Python Coding for Final Year Project

Uploaded by

davidtellez12185
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

OPTIMIZATION , EFFIIENCY

AND SECURITY OF
ENCRYPTION ALGORITHM
BASED ON IMAGE
PROCESSING

PYTHON CODING:

import tkinter as tk
from tkinter import filedialog, messagebox, Label, Button, Frame
from PIL import ImageTk, Image
import cv2
import numpy as np
import os

# User database (for simplicity)


users = {}

class ImageEncryptorApp:
def __init__(self, master):
self.master = master
self.master.title("Image Encryption Decryption")
self.master.attributes('-fullscreen', True)
self.master.bind("<Escape>", self.exit_fullscreen)

self.username = ""
self.unique_key = ""
self.is_sender = None
self.img_path = None
self.original_image = None
self.encrypted_img_path = 'output/image_encrypted.jpg'

# Create directories for input and output images


os.makedirs('input', exist_ok=True)
os.makedirs('output', exist_ok=True)

self.init_login_screen()

def init_login_screen(self):
self.clear_frame()
self.login_frame = Frame(self.master)
self.login_frame.pack()

Label(self.login_frame, text="Login", font=("Times New Roman",


40)).pack(pady=20)

Label(self.login_frame, text="Username", font=("Times New Roman",


20)).pack(pady=5)
self.username_entry = tk.Entry(self.login_frame, font=("Times New
Roman", 20))
self.username_entry.pack(pady=5)

Label(self.login_frame, text="Password", font=("Times New Roman",


20)).pack(pady=5)
self.password_entry = tk.Entry(self.login_frame, show="*",
font=("Times New Roman", 20))
self.password_entry.pack(pady=5)

Label(self.login_frame, text="Unique Key", font=("Times New Roman",


20)).pack(pady=5)
self.key_entry = tk.Entry(self.login_frame, font=("Times New Roman",
20))
self.key_entry.pack(pady=5)

Button(self.login_frame, text="Login", command=self.login_user,


font=("Times New Roman", 20)).pack(pady=10)
Button(self.login_frame, text="Register", command=self.register_user,
font=("Times New Roman", 20)).pack(pady=10)
Button(self.login_frame, text="Exit", command=self.exit_app,
font=("Times New Roman", 20)).pack(pady=10)

def clear_frame(self):
for widget in self.master.winfo_children():
widget.destroy()

def exit_fullscreen(self, event=None):


self.master.attributes('-fullscreen', False)

def exit_app(self):
self.master.quit() # Exit the application

def login_user(self):
username = self.username_entry.get()
password = self.password_entry.get()
unique_key = self.key_entry.get()
if username in users and users[username][0] == password and
users[username][1] == unique_key:
self.username = username
self.unique_key = unique_key
self.init_main_screen()
else:
messagebox.showerror("Error", "Invalid credentials or unique key!")

def register_user(self):
username = self.username_entry.get()
password = self.password_entry.get()
unique_key = self.key_entry.get()

if username in users:
messagebox.showerror("Error", "User already exists!")
else:
users[username] = (password, unique_key)
messagebox.showinfo("Success", "Registration successful!")

def init_main_screen(self):
self.clear_frame()
self.main_frame = Frame(self.master)
self.main_frame.pack()

Label(self.main_frame, text="Choose Role", font=("Times New


Roman", 40)).pack(pady=20)
Button(self.main_frame, text="Sender", command=self.sender_mode,
font=("Times New Roman", 20)).pack(pady=10)
Button(self.main_frame, text="Receiver", command=self.receiver_mode,
font=("Times New Roman", 20)).pack(pady=10)

def sender_mode(self):
self.is_sender = True
self.init_image_screen()

def receiver_mode(self):
self.is_sender = False
self.init_image_screen()

def init_image_screen(self):
self.clear_frame()
Label(self.master, text="Image Encryption/Decryption", font=("Times
New Roman", 40)).pack(pady=20)

self.image_label = Label(self.master)
self.image_label.pack(pady=10)

if self.is_sender:
Button(self.master, text="Choose Image", command=self.open_img,
font=("Times New Roman", 20)).pack(pady=10)
Button(self.master, text="Encrypt", command=self.en_fun,
font=("Times New Roman", 20)).pack(pady=10)
else:
self.display_encrypted_image()
Button(self.master, text="Decrypt", command=self.de_fun,
font=("Times New Roman", 20)).pack(pady=10)

Button(self.master, text="Exit", command=self.exit_to_login,


font=("Times New Roman", 20)).pack(pady=10)

def open_img(self):
self.img_path = filedialog.askopenfilename(title='Open Image',
initialdir='input')
if self.img_path:
img = Image.open(self.img_path)
img.thumbnail((400, 400))
img_display = ImageTk.PhotoImage(img)
self.image_label.configure(image=img_display)
self.image_label.image = img_display
self.image_label['text'] = 'Chosen Image:'
self.original_image = img

os.replace(self.img_path, f'input/{os.path.basename(self.img_path)}')
self.img_path = f'input/{os.path.basename(self.img_path)}'

def en_fun(self):
if not self.img_path:
messagebox.showerror("Error", "No image selected!")
return

image_input = cv2.imread(self.img_path, 0)
(x1, y) = image_input.shape
image_input = image_input.astype(float) / 255.0
mu, sigma = 0, 0.1
key = np.random.normal(mu, sigma, (x1, y)) + np.finfo(float).eps
image_encrypted = image_input / key

cv2.imwrite(self.encrypted_img_path, image_encrypted * 255)

img_enc = Image.open(self.encrypted_img_path)
img_enc.thumbnail((400, 400))
img_enc_display = ImageTk.PhotoImage(img_enc)
self.image_label.configure(image=img_enc_display)
self.image_label.image = img_enc_display
self.image_label['text'] = 'Encrypted Image:'

messagebox.showinfo("Status", "Image Encrypted successfully!")

def display_encrypted_image(self):
if os.path.exists(self.encrypted_img_path):
img_enc = Image.open(self.encrypted_img_path)
img_enc.thumbnail((400, 400))
img_enc_display = ImageTk.PhotoImage(img_enc)
self.image_label.configure(image=img_enc_display)
self.image_label.image = img_enc_display
self.image_label['text'] = 'Encrypted Image:'
else:
messagebox.showerror("Error", "No encrypted image found!")
def de_fun(self):
if self.original_image is not None:
img_dec_display = ImageTk.PhotoImage(self.original_image)
self.image_label.configure(image=img_dec_display)
self.image_label.image = img_dec_display
self.image_label['text'] = 'Decrypted Image:'

messagebox.showinfo("Status", "Image Decrypted successfully!")


else:
messagebox.showerror("Error", "No original image available!")

def exit_to_login(self):
self.init_login_screen()

if __name__ == "__main__":
root = tk.Tk()
app = ImageEncryptorApp(root)
root.mainloop()

OUTPUTS:

You might also like