-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageED.py
112 lines (96 loc) · 3.56 KB
/
ImageED.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
# System imports
from hashlib import sha3_512
import subprocess
# Packages imports
from PIL import Image
class ExtendedImage(object):
def __init__(self, img):
self.img = img
self.width = img.width
self.height = img.height
def encryptimg(self, passkey, pathtosave):
print("Generating Keys for Encryption....")
keys = self.keysgen(passkey)
count = 0
imgpixels = self.img.load()
print("Encrypting Image....")
for i in range(0, self.width):
for j in range(0, self.height):
oi = (keys[count] ^ i) % self.width
oj = (keys[count + 1] ^ j) % self.height
clr = imgpixels[i, j]
imgpixels[i, j] = imgpixels[oi, oj]
imgpixels[oi, oj] = clr
count += 1
if count >= (len(keys) - 1):
count = 0
if pathtosave != "":
print("Saving Image.....")
self.img.save(pathtosave)
def decryptimg(self, passkey, pathtosave):
print("Generating Keys for Decryption....")
keys = self.keysgen(passkey)
imgpixels = self.img.load()
count = ((self.width * self.height) % (len(keys) - 1)) - 1
print("Decrypting Image....")
for i in range(self.width - 1, -1, -1):
for j in range(self.height - 1, -1, -1):
oi = (keys[count] ^ i) % self.width
oj = (keys[count + 1] ^ j) % self.height
clr = imgpixels[i, j]
imgpixels[i, j] = imgpixels[oi, oj]
imgpixels[oi, oj] = clr
count -= 1
if count <= -1:
count = len(keys) - 2
if pathtosave != '':
print("Saving Image....")
self.img.save(pathtosave)
def show(self):
self.img.show()
def keysgen(self, passkey):
nofkeys = min(4095, max(2047, 2 * (max(self.width, self.height))))
keysarr = []
raw = ""
rawhash = sha3_512(passkey.encode()).hexdigest()
rawhash = sha3_512(rawhash[0:64].encode()).hexdigest() + \
sha3_512(rawhash[64:128].encode()).hexdigest()
for i in range(0, len(rawhash)):
raw += bin(ord(rawhash[i]))[2:]
raw = raw[:1602]
i = count = 0
rawlen = len(raw)
keylen = len(bin(max(self.width, self.height)))
while count < nofkeys:
end = (i + keylen)
if end <= (rawlen - 1):
key = raw[i:end]
else:
key = raw[i:rawlen - 1]
key += raw[0:end - rawlen]
keysarr.append(int(key, 2) ^ count)
count += 1
i = (i + keylen) % rawlen
return keysarr
@staticmethod
def genimg(text, path):
strlen = len(text)
arr = text.split(' ')
maxlen = 0
for x in arr:
if len(x) > maxlen:
maxlen = len(x)
if strlen <= 72 and maxlen <= 8:
size = 128
elif strlen <= 256 and maxlen <= 16:
size = 288
elif strlen <= 1280 and maxlen <= 32:
size = 512
else:
size = 1024
size = str(size)
subprocess.call(['scripts/image_generator.sh', size, text, path])
if __name__ == "__main__":
image = ExtendedImage(Image.open("/home/krishna/Desktop/ImageEd/testimages/colored.jpg"))
image.encryptimg("lUUH9d2hg8WEt28Uyg2uHg72", "/home/krishna/Desktop/encrypted.png")
image.decryptimg("lUUH9d2hg8WEt28Uyg2uHg71", "/home/krishna/Desktop/decrypted.png")