-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
322 lines (254 loc) · 11.8 KB
/
project.py
File metadata and controls
322 lines (254 loc) · 11.8 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env python
# ----------------- Header Files ---------------------#
import sys
import random
import argparse
import logging
import os
import math
import hashlib
import binascii
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from tkinter import *
from PIL import Image
from Crypto.Cipher import AES
import numpy as np
global password
def load_image(name):
return Image.open(name)
# ----------------- Functions for encryption ---------------------#
def prepare_message_image(image, size):
if size != image.size:
image = image.resize(size, Image.ANTIALIAS)
return image
def generate_secret(size, secret_image=None):
width, height = size
new_secret_image = Image.new(mode="RGB", size=(width * 2, height * 2))
for x in range(0, 2 * width, 2):
for y in range(0, 2 * height, 2):
color1 = np.random.randint(255)
color2 = np.random.randint(255)
color3 = np.random.randint(255)
new_secret_image.putpixel((x, y), (color1, color2, color3))
new_secret_image.putpixel((x + 1, y), (255 - color1, 255 - color2, 255 - color3))
new_secret_image.putpixel((x, y + 1), (255 - color1, 255 - color2, 255 - color3))
new_secret_image.putpixel((x + 1, y + 1), (color1, color2, color3))
return new_secret_image
def generate_ciphered_image(secret_image, prepared_image):
width, height = prepared_image.size
ciphered_image = Image.new(mode="RGB", size=(width * 2, height * 2))
for x in range(0, width * 2, 2):
for y in range(0, height * 2, 2):
sec = secret_image.getpixel((x, y))
msssg = prepared_image.getpixel((int(x / 2), int(y / 2)))
color1 = (msssg[0] + sec[0]) % 256
color2 = (msssg[1] + sec[1]) % 256
color3 = (msssg[2] + sec[2]) % 256
ciphered_image.putpixel((x, y), (color1, color2, color3))
ciphered_image.putpixel((x + 1, y), (255 - color1, 255 - color2, 255 - color3))
ciphered_image.putpixel((x, y + 1), (255 - color1, 255 - color2, 255 - color3))
ciphered_image.putpixel((x + 1, y + 1), (color1, color2, color3))
return ciphered_image
def generate_image_back(secret_image, ciphered_image):
width, height = secret_image.size
new_image = Image.new(mode="RGB", size=(int(width / 2), int(height / 2)))
for x in range(0, width, 2):
for y in range(0, height, 2):
sec = secret_image.getpixel((x, y))
cip = ciphered_image.getpixel((x, y))
color1 = (cip[0] - sec[0]) % 256
color2 = (cip[1] - sec[1]) % 256
color3 = (cip[2] - sec[2]) % 256
new_image.putpixel((int(x / 2), int(y / 2)), (color1, color2, color3))
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")
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).decode('utf-8')
def replace_all(text, dic):
for i, j in dic.items():
text = text.replace(i, j)
return text
# use replace function to replace ascii cipher characters with numbers
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)
# construct encrypted image
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 a 1
if int(encimageone[len(encimageone) - 1]) < 100:
encimageone[len(encimageone) - 1] += "1"
# check to see if we can divide the string into partitions of 3 digits. if not, fill in with some garbage RGB values
if len(encimageone) % 3 != 0:
while (len(encimageone) % 3 != 0):
encimageone.append("101")
encimagetwo = [(int(encimageone[int(i)]), int(encimageone[int(i + 1)]), int(encimageone[int(i + 2)])) for i in range(0, len(encimageone), step)]
print(len(encimagetwo))
while (int(relength) != len(encimagetwo)):
encimagetwo.pop()
encim = Image.new("RGB", (int(width), int(height)))
encim.putdata(encimagetwo)
encim.save("visual_encrypt.jpeg")
#------------------------- 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"
# convert the plaintext string to bytes
plaintext_bytes = plaintextstr.encode('utf-8')
# convert the IV to bytes and ensure it is 16 bytes long
iv = b'This is an IV456'
# encrypt plaintext
obj = AES.new(password, AES.MODE_CBC, iv)
ciphertext = obj.encrypt(plaintext_bytes)
# write ciphertext to file for analysis
cipher_name = imagename + ".crypt"
with open(cipher_name, 'wb') as g:
g.write(ciphertext)
# Extract the directory from the uploaded image's path
image_directory = os.path.dirname(imagename)
# Save images in the same directory
secret_image = generate_secret(im.size)
secret_image.save(os.path.join(image_directory, "secret.jpeg"))
prepared_image = prepare_message_image(im, im.size)
ciphered_image = generate_ciphered_image(secret_image, prepared_image)
ciphered_image.save(os.path.join(image_directory, "2-share_encrypt.jpeg"))
construct_enc_image(ciphertext, relength, width, height)
print("Visual Encryption done.......")
level_one_encrypt("visual_encrypt.jpeg")
print("2-Share Encryption done.......")
# ---------------------- decryption ---------------------- #
import os
from PIL import Image
from Crypto.Cipher import AES
def decrypt(ciphername, password):
# Extract the directory from the uploaded image's path
image_directory = os.path.dirname(ciphername)
# Open the images (make sure to use the correct paths)
secret_image = Image.open(os.path.join(image_directory, "secret.jpeg"))
ima = Image.open(os.path.join(image_directory, "2-share_encrypt.jpeg"))
# Generate the image back and save it in the same directory
new_image = generate_image_back(secret_image, ima)
new_image_path = os.path.join(image_directory, "2-share_decrypt.jpeg")
new_image.save(new_image_path)
print("2-share Decryption done....")
# Open and read the ciphertext file in binary mode
with open(ciphername, 'rb') as cipher:
ciphertext = cipher.read()
# Convert the IV to bytes and ensure it is 16 bytes long
iv = b'This is an IV456'
# Decrypt ciphertext with password
obj2 = AES.new(password, AES.MODE_CBC, iv)
decrypted = obj2.decrypt(ciphertext)
# Parse the decrypted text back into integer string
decrypted = decrypted.replace(b"n", b"").decode('utf-8')
# Extract dimensions of images
newwidth = decrypted.split("w")[1]
newheight = decrypted.split("h")[1]
# Replace height and width with empty space 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)
# Save the final decrypted image in the same directory
visual_decrypt_path = os.path.join(image_directory, "visual_decrypt.jpeg")
newim.save(visual_decrypt_path)
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')).digest()
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')).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 and Decryption"
author = "Made by Victoria Edwin"
# Set the background color for the entire GUI
master.config(bg='darkgreen')
# Create a frame for layout and set its background color
frame = Frame(master, bg='darkgreen')
frame.pack(fill=BOTH, expand=True)
msgtitle = Message(frame, text=title, bg="darkgreen", fg="white")
msgtitle.config(font=('helvetica', 17, 'bold'), width=200)
msgtitle.pack(pady=10)
msgauthor = Message(frame, text=author, bg="darkgreen", fg="white")
msgauthor.config(font=('helvetica', 10), width=200)
msgauthor.pack(pady=5)
passlabel = Label(frame, text="Enter Encrypt/Decrypt Password:", bg="darkgreen", fg="white")
passlabel.pack(pady=5)
passg = Entry(frame, show="*", width=20)
passg.pack(pady=5)
self.encrypt = Button(frame, text="Encrypt", fg="white", bg="darkorange", font=('helvetica', 12, 'bold'), command=image_open, width=25, height=3, borderwidth=1, relief=RAISED)
self.encrypt.pack(side=LEFT, padx=5, pady=5)
self.decrypt = Button(frame, text="Decrypt", fg="white", bg="darkorange", font=('helvetica', 12, 'bold'), command=cipher_open, width=25, height=3, borderwidth=1, relief=RAISED)
self.decrypt.pack(side=RIGHT, padx=5, pady=5)
# ------------------ MAIN -------------#
root = Tk()
root.wm_title("Image Encryption")
app = App(root)
root.mainloop()