-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgstegano.py
665 lines (451 loc) · 18.4 KB
/
imgstegano.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
import tkinter as tk
from tkinter import ttk, filedialog
import tkinter.simpledialog as sd
import tkinter.messagebox as mb
from PIL import Image, ImageTk
import cv2
import numpy as np
import os
import math
from aes_encryption import AESCipher
integrityCheckString = 'abcde'
# This is used to check the integrity of the file after it is decrypted
# Used to identify if the password entered was correct / incorrect
def dataToBinary(data):
'Converts string, bytes, NumPy array of Integers to Binary or list of bytes represented using strings'
if type(data) == str:
p = ''.join([format(ord(i), '08b')for i in data])
elif type(data) == bytes or type(data) == np.ndarray:
p = [format(i, '08b')for i in data]
return p
def stuffBits(binaryData):
'Adds a 0 after every 6 consecutive 1s (Bit Stuffing). And adds delimiter at the end of binary string data'
bitCount = 0
bitLength = len(binaryData)
bitIndex = 0
while (bitIndex < bitLength - 1):
if binaryData[bitIndex] == '1':
bitCount += 1
if binaryData[bitIndex] == '0':
bitCount = 0
if bitCount == 6:
bitCount = 0
bitIndex += 1
binaryData = binaryData[:bitIndex] + '0' + binaryData[bitIndex:]
bitLength += 1
bitIndex += 1
binaryData += '01111111'
return binaryData
def unstuffBits(binaryData):
'Unstuffs bits from the biinary string and removes the delimiter'
binaryData = binaryData[:-8]
bitCount = 0
bitLength = len(binaryData)
bitIndex = 0
while (bitIndex < bitLength - 1):
if binaryData[bitIndex] == '1':
bitCount += 1
if binaryData[bitIndex] == '0':
bitCount = 0
if bitCount == 6:
bitCount = 0
binaryData = binaryData[:bitIndex+1] + binaryData[bitIndex+2:]
bitLength -= 1
bitIndex += 1
return binaryData
def encode(fileMode, encodeImageFilePath, encodeData, encodedOutputPath, encodeCompress, dynamicRescaling, encodePassword):
'Function implements LSB method to encode data into images'
global integrityCheckString
# Generate the Header
binaryHeader = '000000'
if encodeCompress:
binaryHeader += '1'
else:
binaryHeader += '0'
if encodePassword:
binaryHeader += '1'
else:
binaryHeader += '0'
binaryData = ''
if fileMode:
# Add filename to the header
binaryHeader += dataToBinary(os.path.basename(encodeData))
with open(encodeData, 'rb') as file:
content = file.read()
# Encryption on bytes read
if encodePassword:
# If password is provided (ecryption is to be applied)
encodedByteList = []
for byte in content:
# append is faster in python list than in numpy arrays (or += in strings)
encodedByteList.append(chr(int(byte)))
encodedbyteString = integrityCheckString + ''.join(encodedByteList)
cipher = AESCipher(encodePassword)
encryptedbyteString = cipher.encrypt(encodedbyteString)
binaryData = dataToBinary(encryptedbyteString)
else:
# No encryption
byteStringList = dataToBinary(content)
binaryData = ''.join(byteStringList)
else:
if encodePassword:
# Encryption on text
cipher = AESCipher(encodePassword)
encodeData = integrityCheckString + encodeData
encodeData = cipher.encrypt(encodeData)
binaryData = dataToBinary(encodeData)
# Stuffing hedaer and data seperately. Each will have a seperate delimiter
binaryHeader = stuffBits(binaryHeader)
binaryData = stuffBits(binaryData)
binaryData = binaryHeader + binaryData
'''
We did not go with numpy array of boolean values for binaryData since append is very slow in numpy
and also since we are not doing any operations on the list.
We did not go with list of True / False values as it will not have a significant increase in performance
and will make the program much harder to read and interpret.
'''
image = cv2.imread(encodeImageFilePath)
# Dyanmic Rescaling to store data
dataLength = len(binaryData)
imageBitLength = image.shape[0] * image.shape[1] * 3
if dataLength >= imageBitLength:
if dynamicRescaling:
scalingFactor = math.sqrt(dataLength/imageBitLength)
height = math.ceil(image.shape[0] * scalingFactor)
width = math.ceil(image.shape[1] * scalingFactor)
dim = (width, height)
image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
else:
mb.showinfo('Dynamic Rescaling', 'The data to be encoded is too large. Enable Dynamic Rescaling to scale the image to fit the data.')
return -1
dataIndex = 0
# Modifying the Least Significant Bit of each component of each pixel to store bits of Binary Data
for row in image:
for pixel in row:
if dataIndex >= dataLength:
break
if dataIndex < dataLength:
pixel[0] = int(pixel[0] & 0b11111110) + int(binaryData[dataIndex])
dataIndex += 1
if dataIndex < dataLength:
pixel[1] = int(pixel[1] & 0b11111110) + int(binaryData[dataIndex])
dataIndex += 1
if dataIndex < dataLength:
pixel[2] = int(pixel[2] & 0b11111110) + int(binaryData[dataIndex])
dataIndex += 1
cv2.imwrite(encodedOutputPath, image)
mb.showinfo('Encoding complete', f'Output generated: {encodedOutputPath}')
def decode(decodeImageFilePath, decodeDestination=''):
global integrityCheckString
compressed = False
protected = False
delimsFound = 0
bitCount = 0
binaryHeader = ''
binaryData = ''
readBitsList = []
image = cv2.imread(decodeImageFilePath)
# Reading the Least Significant Bit of each component of each pixel
for row in image:
# The reading stops when both the delimiters (header and data) are found
if delimsFound == 2:
break
for pixel in row:
b = pixel[0] & 1
g = pixel[1] & 1
r = pixel[2] & 1
readBitsList.append(b)
if b:
bitCount += 1
if bitCount == 7:
delimsFound += 1
# When the first delimiter is found, the data read is stored in binaryHeader
if delimsFound == 1:
binaryHeader = ''.join(map(str, readBitsList))
readBitsList = []
if delimsFound == 2:
break
else:
bitCount = 0
readBitsList.append(g)
if g:
bitCount += 1
if bitCount == 7:
delimsFound += 1
if delimsFound == 1:
binaryHeader = ''.join(map(str, readBitsList))
readBitsList = []
if delimsFound == 2:
break
else:
bitCount = 0
readBitsList.append(r)
if r:
bitCount += 1
if bitCount == 7:
delimsFound += 1
if delimsFound == 1:
binaryHeader = ''.join(map(str, readBitsList))
readBitsList = []
if delimsFound == 2:
break
else:
bitCount = 0
binaryData = ''.join(map(str, readBitsList))
binaryHeader = unstuffBits(binaryHeader)
# Header is read to get the options
if binaryHeader[6] == '1':
compressed = True
if binaryHeader[7] == '1':
protected = True
binaryHeader = binaryHeader[8:]
byteHeader = [binaryHeader[i: i + 8] for i in range(0, len(binaryHeader), 8)]
# Filename is extracted from the header
fileName = ""
for x in byteHeader:
fileName += chr(int(x, 2))
binaryData = unstuffBits(binaryData)
byteStringList = [binaryData[i: i + 8] for i in range(0, len(binaryData), 8)]
if fileName:
# File Mode (encoded data is a file)
byteData = []
if protected:
# If data is password protected (to be decrypted)
encryptedDataList = []
for byte in byteStringList:
encryptedDataList.append(chr(int(byte, 2)))
encryptedDataString = ''.join(encryptedDataList)
password = sd.askstring("Password", "Enter Password")
cipher = AESCipher(password)
try:
decryptedDataString = cipher.decrypt(encryptedDataString)
except UnicodeDecodeError:
mb.showerror("Error", "Incorrect Password!")
return -1
# try except blocks do not introduce a new scope
if decryptedDataString[0:5] != integrityCheckString:
mb.showerror("Error", "Incorrect Password!")
return -1
else:
decryptedDataString = decryptedDataString[5:]
for char in decryptedDataString:
byteData.append(ord(char))
else:
# No decryption
for byte in byteStringList:
byteData.append(int(byte, 2))
byteData = bytearray(byteData)
fullPath = os.path.join(decodeDestination, fileName)
with open( fullPath , 'wb') as file:
file.write(byteData)
mb.showinfo('Decoding Complete', 'Output generated at: ' + fullPath)
else:
# Text Mode (encoded data is a message)
if protected:
# Password protected (decryption)
encryptedDataList = []
for x in byteStringList:
encryptedDataList.append(chr(int(x, 2)))
encryptedData = ''.join(encryptedDataList)
password = sd.askstring("Password", "Enter Password")
cipher = AESCipher(password)
try:
decryptedData = cipher.decrypt(encryptedData)
except UnicodeDecodeError:
mb.showerror("Error", "Incorrect Password!")
return -1
if decryptedData[0:5] != integrityCheckString:
mb.showerror("Error", "Incorrect Password!")
return -1
else:
decryptedData = decryptedData[5:]
decodedTextBox.insert('1.0', decryptedData)
else:
# No password protection
dataList = []
for x in byteStringList:
dataList.append(chr(int(x, 2)))
data = ''.join(dataList)
decodedTextBox.insert('1.0', data)
decodedTextBox.pack(pady=5, padx=10)
'''
/////////////////////
GUI Code ///////////
///////////////////
'''
window = tk.Tk()
window.geometry("600x800")
window_height = 800
window.title("Image Encoder/Decoder")
# Global variables to store File Paths
encodeImageFilePath = ''
decodeImageFilePath = ''
encodeData = ''
decodeDestination = ''
def chooseImageEncode():
'Handles chosing image to be encoded'
global encodeImageFilePath
filetypes = [("PNG files", "*.png"), ("All files", "*.*")]
encodeImageFilePath = filedialog.askopenfilename(filetypes=filetypes)
if encodeImageFilePath:
# Scales the image to have 40% of window height
image = Image.open(encodeImageFilePath)
width, height = image.size
maxHeight = int(window_height * 0.4)
if height > maxHeight:
width = int(width * (maxHeight / height))
height = maxHeight
image = image.resize((width, height))
imageEncode = ImageTk.PhotoImage(image)
imageBoxEncode.configure(image=imageEncode)
imageBoxEncode.image = imageEncode # Setting a reference
chooseImageButtonEncode.configure(text="Choose another image")
def chooseEncodeFile():
'Handles chossing a file to be encoded in the selected image'
global encodeData
filetypes = [("All files", "*.*")]
encodeData = filedialog.askopenfilename(filetypes=filetypes)
if encodeData:
fileLabel.configure(text=encodeData)
def outputDestinationChange():
'Handles change in destination of output files generated after decoding'
global decodeDestination
decodeDestination = filedialog.askdirectory()
if decodeDestination:
destinationLabel.configure(text=f'Output File Destination: {decodeDestination}')
def chooseImageDecode():
'Handles choosing the image to be decoded'
global decodeImageFilePath
filetypes = [("PNG files", "*.png"), ("All files", "*.*")]
decodeImageFilePath = filedialog.askopenfilename(filetypes=filetypes)
if decodeImageFilePath:
# Scales the image to fit in 40% of window height
image = Image.open(decodeImageFilePath)
width, height = image.size
maxHeight = int(window_height * 0.4)
if height > maxHeight:
width = int(width * (maxHeight / height))
height = maxHeight
image = image.resize((width, height))
imageDecode = ImageTk.PhotoImage(image)
imageBoxDecode.configure(image=imageDecode)
imageBoxEncode.image = imageDecode # Setting a reference
chooseImageButtonDecode.configure(text="Choose another image")
def onEncode():
'Validates input and calls the encode method when the user clicks on the Encode button'
global encodeImageFilePath, encodeData
# Encode Parameters
fileMode = False
if not encodeImageFilePath:
mb.showinfo('Select an Image', 'Select an image to encode data into')
if encodeSubtabs.tab(encodeSubtabs.select(), 'text') == 'File':
fileMode = True
if not encodeData:
mb.showinfo('No File Selected', 'Please select a file to encode in the image')
return -1
else:
encodeData = textBox.get('1.0', 'end-1c')
if not encodeData:
mb.showinfo('Empty Message', 'Please enter a message to encode')
encodedOutputPath = outputEntry.get()
encodeCompress = compressionVar.get()
dynamicRescaling = rescalingVar.get()
encodePassword = passwordEntry.get()
# Hide the Encode button and display "Encoding..." label
encodeButton.pack_forget()
encodingLabel.pack(pady=10)
encode(fileMode, encodeImageFilePath, encodeData, encodedOutputPath, encodeCompress, dynamicRescaling, encodePassword)
# Unhide the Encode button
encodeButton.pack(pady=10)
encodingLabel.pack_forget()
def onDecode():
'Calls the decode method when the user clicks on Decode button'
global decodeImageFilePath, decodeDestination
if not decodeImageFilePath:
mb.showinfo('Select an Image', 'Select an image to decode')
return -1
decodedTextBox.pack_forget()
# Hide the Decode button and display "Decoding..." label
decodeButton.pack_forget()
decodingLabel.pack(pady=10)
decode(decodeImageFilePath, decodeDestination)
# Make the Decode button visible again
decodingLabel.pack_forget()
decodeButton.pack(pady=5)
# Create tabbed group with 2 tabs
tabs = ttk.Notebook(window)
tabs.pack(fill='both', expand=True)
tabs.enable_traversal()
# Create encode tab
encodeTab = ttk.Frame(tabs)
tabs.add(encodeTab, text="Encode")
imageBoxEncode = tk.Label(encodeTab)
imageBoxEncode.pack()
# Create choose image button and image box
chooseImageButtonEncode = tk.Button(encodeTab, text="Choose an Image", command=chooseImageEncode)
chooseImageButtonEncode.pack(pady=10)
# Create tabbed group with 2 tabs (File and Text)
encodeSubtabs = ttk.Notebook(encodeTab, height=100)
encodeSubtabs.pack()
# Create Text tab
textTab = ttk.Frame(encodeSubtabs)
encodeSubtabs.add(textTab, text="Text")
textBox = tk.Text(textTab)
textBox.pack(pady=10, padx=20)
# Create File tab
fileTab = ttk.Frame(encodeSubtabs)
encodeSubtabs.add(fileTab, text="File")
fileLabel = tk.Label(fileTab, text="No file chosen")
fileLabel.pack(pady=10)
browseButton = tk.Button(fileTab, text="Browse", command=chooseEncodeFile)
browseButton.pack(pady=10)
# Create password entry box
passwordFrame = tk.Frame(encodeTab)
passwordFrame.pack(side=tk.TOP, padx=5, pady=5)
passwordLabel = tk.Label(passwordFrame, text="Password:")
passwordLabel.pack(side=tk.LEFT)
passwordEntry = tk.Entry(passwordFrame, width=30)
passwordEntry.pack(side=tk.LEFT)
# Create output path entry
outputFrame = tk.Frame(encodeTab)
outputFrame.pack(side=tk.TOP, padx=5, pady=5)
outputLabel = tk.Label(outputFrame, text="Output:")
outputLabel.pack(side=tk.LEFT)
outputEntry = tk.Entry(outputFrame, width=30)
outputEntry.insert(0, 'out.png')
outputEntry.pack(side=tk.LEFT)
# Create compression and dynamic rescaling checkboxes
compressionVar = tk.BooleanVar()
compressionCheck = tk.Checkbutton(encodeTab, text="Compression", variable=compressionVar)
compressionCheck.pack(pady=10)
rescalingVar = tk.BooleanVar()
dynamicCheck = tk.Checkbutton(encodeTab, text="Dynamic Rescaling", variable=rescalingVar)
dynamicCheck.pack()
# Create encode button
encodeButton = tk.Button(encodeTab, text="Encode", command=onEncode)
encodeButton.pack(pady=10)
# Create Encoding Label
encodingLabel = tk.Label(encodeTab, text="Encoding...")
# Create decode tab
decodeTab = ttk.Frame(tabs)
tabs.add(decodeTab, text="Decode")
imageBoxDecode = tk.Label(decodeTab)
imageBoxDecode.pack()
# Create choose image button for decoding
chooseImageButtonDecode = tk.Button(decodeTab, text="Choose an Image", command=chooseImageDecode)
chooseImageButtonDecode.pack(pady=10)
decodedTextBox = tk.Text(decodeTab, height=5)
# Create decode destination path widget
destinationFrame = tk.Frame(decodeTab)
destinationFrame.pack(side=tk.TOP, padx=5, pady=5)
destinationLabel = tk.Label(destinationFrame, text="Output File Destination: Current Directory")
destinationLabel.pack(side=tk.LEFT)
changeButton = tk.Button(destinationFrame, text="Browse", command=outputDestinationChange)
changeButton.pack(side=tk.LEFT)
# Create decode button
decodeButton = tk.Button(decodeTab, text="Decode", command=onDecode)
decodeButton.pack(pady=10)
# Create Decoding Label
decodingLabel = tk.Label(decodeTab, text="Decoding...")
# Start main loop
window.mainloop()