-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecrypt.py
125 lines (89 loc) · 3.56 KB
/
decrypt.py
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
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Program : decrypt.py
Description : This program takes an encrypted image, public key image,
and private key image then performs decryption algorithm
to extract the message that was encrypted.
Author : Burak Can Ozter - B00784243 , [email protected]
Date : March 19th, 2019
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' LIBRARIES '''
from PIL import Image
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''
Debug
'''
encrypted_image_pixels = open("encryptedPixels.txt","w+")
message_pixels = open("messagePixels.txt","w+")
pub_pixels = open("pubPixels.txt","w+")
private_pixels = open("privatePixels.txt","w+")
decrypted_message = open("decryptedMessage.txt","w+")
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Open images and load them into memory '''
EncryptedImage = Image.open('enc.png')
encRGB = EncryptedImage.convert("RGB")
privateKey = Image.open('pk.png')
PK = privateKey.resize((256,256),Image.ANTIALIAS)
priRGB = PK.convert("RGB")
publicKey = Image.open('pub.png')
PUK = publicKey.resize((256,256),Image.ANTIALIAS)
publRGB = PUK.convert("RGB")
''' Initialize a 2d array to save decrypted pixels'''
decryptedPixel = [[]]
decryptedPixel = [[0 for i in range(256)] for i in range(256)]
''' Start processing images '''
for i in range(0,256):
for j in range(0,256):
''' Get the pixel value at a given position '''
pubR,pubG,pubB = publRGB.getpixel((i,j))
encR,encG,encB = encRGB.getpixel((i,j))
priR,priG,priB = priRGB.getpixel((i,j))
''' DEBUG '''
encrypted_image_pixels.write("Location: %d,%d\nR: %d G: %d B: %d\n" % (i, j, encR, encG, encB))
pub_pixels.write("Location: %d,%d\nR: %d G: %d B: %d\n" % (i, j, pubR, pubG, pubB))
private_pixels.write("Location: %d,%d\nR: %d G: %d B: %d\n" % (i, j, priR, priG, priB))
''''''
''' Decryption Algorithm '''
msgR = int(int(encR) - int(pubR/2) - int(priR/2))
msgG = int(int(encG) - int(pubG/2) - int(priG/2))
msgB = int(int(encB) - int(pubB/2) - int(priB/2))
if( msgR < 0):
msgR = msgR + 255
if( msgG < 0):
msgG = msgG + 255
if( msgB < 0):
msgB = msgB + 255
''' Save the decoded RGB values into a list to access later '''
decryptedPixel[i][j] = int(msgR),int(msgG),int(msgB)
''' DEBUG '''
#if (msgR != 0 and msgG != 0 and msgB != 0):
message_pixels.write("Location: %d,%d\nR: %d G: %d B: %d\n" % (i, j, msgR, msgG, msgB))
''''''
''' Message extraction algorithm '''
msgCount = 0
Xa = 0
Ya = 0
while(True):
message = decryptedPixel[Xa][Ya][msgCount%3]
newXa = decryptedPixel[Xa][Ya][(msgCount+1)%3]
Ya = decryptedPixel[Xa][Ya][(msgCount+2)%3]
Xa = newXa
msgCount = msgCount + 1
if(Xa == 0 and Ya == 0):
break
print("%c" %(message),end = "")
decrypted_message.write("%c" % (message))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''
DEBUG
'''
message_pixels.close()
encrypted_image_pixels.close()
pub_pixels.close()
private_pixels.close()
decrypted_message.close()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Close file pointer and release its memory '''
EncryptedImage.close()
publicKey.close()
privateKey.close()
''' END OF PROGRAM '''