-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
54 lines (42 loc) · 1.72 KB
/
main.py
File metadata and controls
54 lines (42 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from tkinter import *
from tkinter import filedialog
root = Tk() #main window
root.geometry("300x200") #width X height
root.title("Image Encryptor/Decryptor") #title
def encrypt_image():
perform_encryption(True) #wrapper
def decrypt_image():
perform_encryption(False)
def perform_encryption(is_encrypt):
file_path = filedialog.askopenfilename(filetypes=[('JPG files', '*.jpg')])
if file_path:
try:
key = int(key_entry.get())
with open(file_path, 'rb') as file:
image_data = bytearray(file.read())
# XOR operation for both encryption and decryption
for index, value in enumerate(image_data):
image_data[index] = value ^ key
# Save the modified image
with open(file_path, 'wb') as file:
file.write(image_data)
action = "Encrypted" if is_encrypt else "Decrypted"
label_status.config(text=f"{action} Successfully")
except ValueError:
label_status.config(text="Invalid key. Please enter a number.")
except Exception as e:
label_status.config(text="Error: " + str(e))
finally:
# Clear the entry widget for the key after operation
key_entry.delete(0, END)
label_key = Label(root, text="Enter Encryption Key (number):")
label_key.place(x=10, y=10)
key_entry = Entry(root)
key_entry.place(x=180, y=10)
encrypt_button = Button(root, text="Encrypt", command=encrypt_image)
encrypt_button.place(x=50, y=50)
decrypt_button = Button(root, text="Decrypt", command=decrypt_image)
decrypt_button.place(x=150, y=50)
label_status = Label(root, text="")
label_status.place(x=10, y=100)
root.mainloop()