-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryptionCode.py
422 lines (359 loc) · 15.3 KB
/
encryptionCode.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
## Image Encryption, watermark, etc code starts .
try:
import cv2
except:
print('Please install OpenCV first to run this!')
exit(1)
try:
from PyQt5 import QtCore
from PyQt5 import uic
from PyQt5.QtCore import pyqtSlot, QTimer
from PyQt5.QtGui import QImage, QPixmap, QIcon
from PyQt5.QtWidgets import QFileDialog, QApplication, QMessageBox, QAction, QMainWindow
except:
print("Please install PyQt5 first to run the interface!")
exit(1)
try:
import matplotlib.pyplot as plt
except:
print('Please install matplotlib, some features like [Histograms] will not work')
def getChar(self, binChar):
binChar = reversed(binChar)
mul = int(1)
sum = int(0);
for i in binChar:
sum += (ord(i) - ord('0')) * mul
mul *= int(2)
return str(chr(sum))
def getNumFromBin(self, binaryThing):
binaryThing = reversed(binaryThing)
mul = int(1)
sum = int(0);
for i in binaryThing:
sum += (ord(i) - ord('0')) * mul
mul *= int(2)
return sum
def binary2String(self, myBinString):
i = 0
# myBinString='01100001'
retString = ''
l = len(myBinString)
while i < l:
binChar = myBinString[i:i + 8]
ch = self.getChar(binChar)
retString += ch
i += 8;
return retString
def string2Binary(self, myString):
# myString = 'This is me'
binString = ''
for i in myString:
a = ord(i)
bitstring = bin(a)
bitstring = bitstring[2:]
bitstring = -len(bitstring) % 8 * '0' + bitstring
binString += bitstring
return binString
def waterMarkImageClicked(self, watermark=' This Image is Encrypted! '):
# iterate over the matrix channel 1 and change some of the values.
self.encryptionCodeHelper()
self.lastFilter = 'Image Encryption'
self.initializeSlider()
print('Encrypting:')
rows = 0
cols = 0
channel = 0
if not watermark:
watermark = ' This Image is Encrypted ' # rarest of the rare case, that they ever come together in a random
# Image.
index = 0
watermarkBin = self.string2Binary(watermark)
if len(self.processedImage.shape) == 2:
rows, cols = self.processedImage.shape
else:
rows, cols, channel = self.processedImage.shape
# for stopping once the file has ended so that I dont apply watermark again and again and again( if the water
# mark is appended with the end of file string)
if len(self.processedImage.shape) == 2:
index = 0
for row in range(rows):
for col in range(cols):
num = self.processedImage[row][col]
# convert the num to binary
bitstring = bin(num)
bitstring = bitstring[2:]
bitstring = -len(bitstring) % 8 * '0' + bitstring
# after converting it to binary change the last positions of the image number
temp = bitstring[0:6]
if index + 1 < len(watermarkBin):
temp += watermarkBin[index:index + 2]
index += 2
else:
index = 0
index = index % len(watermarkBin)
self.processedImage[row][col] = self.getNumFromBin(temp)
elif len(self.processedImage.shape) == 3:
index = 0
for row in range(rows):
for col in range(cols):
num = self.processedImage[row][col][0]
# convert the num to binary
bitstring = bin(num)
bitstring = bitstring[2:]
bitstring = -len(bitstring) % 8 * '0' + bitstring
temp = ''
temp += bitstring[0:6]
# after converting it to binary change the last positions of the image number, the green channel has
# all the data
if index + 1 < len(watermarkBin):
temp += watermarkBin[index]
temp += watermarkBin[index + 1]
index += 2
else:
index = 0
index = index % len(watermarkBin)
self.processedImage[row][col][0] = self.getNumFromBin(temp)
if len(self.processedImage.shape) > 0:
self.displayImage(2)
print('WaterMarked! Click Update original and then save to save this encrypted image on disk.')
# newest function in the line
def encryptImageHelper(self, data):
# iterate over the matrix channel 1 and change some of the values.
self.encryptionCodeHelper()
self.lastFilter = 'Image Encryption'
self.initializeSlider()
data += '\n\n!@#$%^&*' # this is faster than normal and there is a very low chance of getting this in any
# expression continuously!
print('Encrypting:')
rows = 0
cols = 0
channel = 0
myEndFlag = 0
if not data:
data = ' This Image is Encrypted ' # rarest of the rare case, that they ever come together in a random Image.
binaryData = self.string2Binary(data) # convert the data to binary
if len(self.processedImage.shape) == 2:
rows, cols = self.processedImage.shape
else:
rows, cols, channel = self.processedImage.shape
# for stopping once the file has ended so that I dont apply watermark again and again and again( if the water
# mark is appended with the end of file string)
if len(self.processedImage.shape) == 2:
index = 0
for row in range(rows):
for col in range(cols):
num = self.processedImage[row][col]
# convert the num to binary
bitstring = bin(num)
bitstring = bitstring[2:]
bitstring = -len(bitstring) % 8 * '0' + bitstring
# after converting it to binary change the last positions of the image number
temp = bitstring[0:6]
if index + 1 < len(binaryData):
temp += binaryData[index:index + 2]
index += 2
else:
index = 0
self.processedImage[row][col] = self.getNumFromBin(temp)
myEndFlag = 1
break
# index = index % len(binaryData) # we dont want to do that the index circles around the image.
# Instread we want to break it.
self.processedImage[row][col] = self.getNumFromBin(temp)
if myEndFlag == 1:
break
elif len(self.processedImage.shape) == 3:
index = 0
for row in range(rows):
for col in range(cols):
num = self.processedImage[row][col][0]
# convert the num to binary
bitstring = bin(num)
bitstring = bitstring[2:]
bitstring = -len(bitstring) % 8 * '0' + bitstring
temp = ''
temp += bitstring[0:6]
# after converting it to binary change the last positions of the image number, the green channel has
# all the data
if index + 1 < len(binaryData):
temp += binaryData[index]
temp += binaryData[index + 1]
index += 2
else:
index = 0
self.processedImage[row][col] = self.getNumFromBin(temp)
myEndFlag = 1
break
# index = index % len(binaryData)
self.processedImage[row][col][0] = self.getNumFromBin(temp)
if myEndFlag == 1:
break
if len(self.processedImage.shape) > 0:
self.displayImage(2)
print('WaterMarked! Click Update original and then save to save this encrypted image on disk.')
def bringEncryptedDataFromImage(
self): # is data flag was true , then the function stores data else it stores the watermark only.
self.encryptionCodeHelper()
print('Decrypting the Image :\n')
rows = 0
cols = 0
channel = 0
index = 0
# checks the end of the data stream.
endChecker = ''
dataFromImage = '' # this will contain a binary string that will be read from the image
dataFromImageReturn = ''
if len(self.processedImage.shape) == 2:
rows, cols = self.processedImage.shape
else:
rows, cols, channel = self.processedImage.shape
# tempFlag = False
timeToReturn = False # returns if it detects that the endOfFile we added to the image at encryption >> !@#$%^&*
if len(self.processedImage.shape) == 2:
for row in range(rows):
for col in range(cols):
num = self.processedImage[row][col]
# convert the num to binary
bitstring = bin(num)
bitstring = bitstring[2:]
bitstring = -len(bitstring) % 8 * '0' + bitstring
# after converting it to binary change the last positions of the image number
dataFromImage += bitstring[6:8]
dataFromImageReturn += bitstring[6:8]
if len(dataFromImage) >= 8: # to check the end of the file statement from data flag
if len(endChecker) == 7:
if endChecker == '!@#$%^&':
timeToReturn = True
break
else:
endChecker = endChecker[1:]
endChecker += self.binary2String(dataFromImage)
elif len(endChecker) < 7:
endChecker += self.binary2String(dataFromImage)
else:
endChecker = ''
print(self.binary2String(dataFromImage), end='')
dataFromImage = ''
if timeToReturn:
break
elif len(self.processedImage.shape) == 3:
for row in range(rows):
for col in range(cols):
num = self.processedImage[row][col][0]
# convert the num to binary
bitstring = bin(num)
bitstring = bitstring[2:]
bitstring = -len(bitstring) % 8 * '0' + bitstring
# after converting it to binary change the last positions of the image number
dataFromImage += bitstring[6:8]
dataFromImageReturn += bitstring[6:8]
if len(dataFromImage) >= 8: # to check the end of the file statement from data flag
if len(endChecker) == 7:
if endChecker == '!@#$%^&':
timeToReturn = True
break
else:
endChecker = endChecker[1:]
endChecker += self.binary2String(dataFromImage)
elif len(endChecker) < 7:
endChecker += self.binary2String(dataFromImage)
else:
endChecker = ''
print(self.binary2String(dataFromImage), end='')
dataFromImage = ''
if timeToReturn:
break
print('\n\nThe Image was Successfully Scanned!')
# so I got the watermark, now time to print the watermark!
# print(self.binary2String(imageWatermark[:]))
return self.binary2String(dataFromImageReturn)[:-8]
def checkWatermark(
self): # is data flag was true , then the function stores data else it stores the watermark only.
self.encryptionCodeHelper()
print('WaterMarking /Decrypting the Image:')
rows = 0
cols = 0
channel = 0
index = 0
imageWatermark = '' # this will contain a binary string that will be read from the image
imageWatermarkReturn = ''
if len(self.processedImage.shape) == 2:
rows, cols = self.processedImage.shape
else:
rows, cols, channel = self.processedImage.shape
# tempFlag = False
if len(self.processedImage.shape) == 2:
for row in range(rows):
for col in range(cols):
num = self.processedImage[row][col]
# convert the num to binary
bitstring = bin(num)
bitstring = bitstring[2:]
bitstring = -len(bitstring) % 8 * '0' + bitstring
# after converting it to binary change the last positions of the image number
imageWatermark += bitstring[6:8]
imageWatermarkReturn += bitstring[6:8]
if len(imageWatermark) >= 8: # to check the end of the file statement from data flag
print(self.binary2String(imageWatermark), end='')
imageWatermark = ''
elif len(self.processedImage.shape) == 3:
for row in range(rows):
for col in range(cols):
num = self.processedImage[row][col][0]
# print('here')
# convert the num to binary
bitstring = bin(num)
bitstring = bitstring[2:]
bitstring = -len(bitstring) % 8 * '0' + bitstring
# after converting it to binary change the last positions of the image number
# imageWatermark+=bitstring[6:8]
imageWatermark += bitstring[6:8]
imageWatermarkReturn += bitstring[6:8]
# if len(imageWatermark) >= 8: #old working code without end of the file
# print(self.binary2String(imageWatermark),end='')
# imageWatermark=''
if len(imageWatermark) >= 8: # to check the end of the file statement from data flag
print(self.binary2String(imageWatermark), end='')
imageWatermark = ''
print('\n\nThe image was successfully scanned')
# so I got the watermark, now time to print the watermark!
# print(self.binary2String(imageWatermark[:]))
return self.binary2String(imageWatermarkReturn)
def encryptDataClicked(self):
self.encryptionCodeHelper()
fname, _ = QFileDialog.getOpenFileName(self, 'Open Text File', '', 'Text File (*.txt)')
data = None
myfile = None
try:
if fname:
with open(fname, encoding="utf8", errors='ignore') as myfile:
data = myfile.read()
data=str(data)
# print(data)
# self.waterMarkImageClicked(data)
self.encryptImageHelper(data)
myfile.close()
else:
print("Please reselect a valid file!")
except Exception as e:
print(e)
print("Invalid file format and/or you need to input a text file")
print("")
def decryptDataClicked(self):
self.encryptionCodeHelper()
print('Decrypting: ')
fname, _ = QFileDialog.getOpenFileName(self, 'Open File to save', '', 'Text File (*.txt)')
data = None
myfile = None
if fname:
with open(fname, 'w') as myfile:
# myfile.write(self.checkWatermark())
myfile.write(self.bringEncryptedDataFromImage())
myfile.close()
else:
print('Please reselect a valid file!')
## Image Encryption code ends
# This is the functions for the decorators in the main file of Photoshop. This can be used to send functions to the
# main file!
functions = (getChar, getNumFromBin, binary2String, string2Binary, waterMarkImageClicked, encryptImageHelper,
bringEncryptedDataFromImage, checkWatermark, encryptDataClicked, decryptDataClicked)