-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage Steg.py
More file actions
251 lines (236 loc) · 15.9 KB
/
Copy pathImage Steg.py
File metadata and controls
251 lines (236 loc) · 15.9 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
import cv2
import time
def encrypt(binary):
imgfile = input("Input the file name you would like to encode. (Only accepts .png. Do not include extension. Must be same directory as file)") #ask user to encode
try:
img = cv2.imread("{}.png".format(imgfile), cv2.IMREAD_COLOR) #load file to encode
except:
print("Invalid Input Exiting")
time.sleep(2.5)
exit()
#print(img[1][1])
H = int(0)
Count = int(0) #varible setup
End = False
for i, row in enumerate(img): #get pixel values by iterating in a cross pattern
for j, pixel in enumerate(img):
if (i == j or i + j == img.shape[0]):
for i in range(len(binary)): #loop for each char in the encoding sentence
binary1 = binary[:3] #get the first 3 char of the encoded binary message
binary = binary[3:] #remove the first 3 chars from the encoded binary message
r, g, b = img[i][j] #get RGB for the specfic pixel location
binary1 = list(binary1)
#print(r)
#print(g) #debuging prints
#print(b)
#print(binary1)
while H != 3 and End == False: #ensuring r g b iteration
try:
V = (binary1[0]) #ensuring list length (8bits dosnt fit in 3)
except:
End = True
H = 3
if H == 0 and End == False:
if (binary1[H] == '0' and r % 2 != 0):
r = r - 1 #change rgb value to corraspond to the binary odd or even
H = H + 1 #adding to pixel count
Count = Count + 1 #adding to master overall count
#print("after {}".format(r))
elif (binary1[H] == '1' and r % 2 == 0):
if (r != 0):
r = r - 1 #change rgb value to corraspond to the binary odd or even
H = H + 1 #adding to pixel count
Count = Count + 1 #adding to master overall count
#print("after {}".format(r))
else:
r = r + 1 #change rgb value to corraspond to the binary odd or even
H = H + 1 #adding to pixel count
Count = Count + 1 #adding to master overall count
#print("after {}".format(r))
else:
H = H + 1 #adding to pixel count
Count = Count + 1 #adding to master overall count
try:
V = (binary1[1]) #ensuring list length (8bits dosnt fit in 3)
except:
End = True
elif H == 1 and End == False:
try:
V = (binary1[1]) #ensuring list length (8bits dosnt fit in 3)
except:
End = True
break
if (binary1[H] == '0' and g % 2 != 0):
g = g - 1 #change rgb value to corraspond to the binary odd or even
H = H + 1 #adding to pixel count
Count = Count + 1 #adding to master overall count
#print("after {}".format(g))
elif (binary1[H] == '1' and g % 2 == 0):
if (g != 0):
g = g - 1 #change rgb value to corraspond to the binary odd or even
H = H + 1 #adding to pixel count
Count = Count + 1 #adding to master overall count
#print("after {}".format(g))
else:
g = g + 1 #change rgb value to corraspond to the binary odd or even
H = H + 1 #adding to pixel count
Count = Count + 1 #adding to master overall count
#print("after {}".format(g))
else:
H = H + 1 #adding to pixel count
Count = Count + 1 #adding to master overall count
elif H == 2 and End == False:
try:
V = (binary1[2]) #ensuring list length (8bits dosnt fit in 3)
except:
End = True
break
if (binary1[H] == '0' and b % 2 != 0):
b = b - 1 #change rgb value to corraspond to the binary odd or even
H = H + 1 #adding to pixel count
Count = Count + 1 #adding to master overall count
#print("after {}".format(b))
elif (binary1[H] == '1' and b % 2 == 0):
if (b != 0):
b = b - 1 #change rgb value to corraspond to the binary odd or even
H = H + 1 #adding to pixel count
Count = Count + 1 #adding to master overall count
# print("after {}".format(b))
else:
b = b + 1 #change rgb value to corraspond to the binary odd or even
H = H + 1 #adding to pixel count
Count = Count + 1 #adding to master overall count
# print("after {}".format(b))
else:
H = H + 1 #adding to pixel count
Count = Count + 1 #adding to master overall count
H = 0
#print("{} {} {} Being Wrote to {}".format(r, g, b, img[i][j])) Debugging String to ensure the pixels are being edited correctly
img[i][j] = [r, g, b] #Setting the edited rgb values to the orginal pixel location
#print("{} {} {} Being Wrote to {}".format(r,g,b,img[i][j])) Debugging String to ensure the pixels are being edited correctly
if End == True:
print("Decoding Concluded")
print("Your Key is {}. THIS IS REQUIRED TO DECRYPT".format(Count)) # Supplying end user with the key
cv2.imwrite("{}_encoded.png".format(imgfile), img) #Creating the encoding image file
print("The encoded image is {}_encoded.png and is saved in the same directory as this python file".format(imgfile))
return Count
def decrypt(Count):
H = int(0)
Key = int(0) #varible setup
decoded = str("")
imgfile = input("Input the file name you would like to decode. (Only accepts .png. Do not include extension. Must be same directory as file)") #ask user to point to file to decode
try:
img = cv2.imread("{}.png".format(imgfile), cv2.IMREAD_COLOR) #Open image file
except:
print("Invalid Input Exiting")
time.sleep(2.5)
exit()
for i, row in enumerate(img): #get pixel values by iterating in a cross pattern
for j, pixel in enumerate(img):
if (i == j or i + j == img.shape[0]):
for i in range(Count): #loop for each char in the encoding sentence
r, g, b = img[i][j] #get RGB for the specfic pixel location
if Count == Key:
return decoded
while H != 3: #ensuring r g b iteration
if H == 0:
if r % 2 == 0: #if r even
A = 0 #setting binary bit to 0 or 1 depending if r g b is odd or even
H = H + 1 #adding to pixel count
Key = Key + 1 #adding to key count
if Count == Key:
decoded = decoded + str(A)#add the extracted binary translation to the final decoded string
return decoded
else:
A = 1 #setting binary bit to 0 or 1 depending if r g b is odd or even
H = H + 1 #adding to pixel count
Key = Key + 1 #adding to key count
if Count == Key:
decoded = decoded + str(A)#add the extracted binary translation to the final decoded string
return decoded
elif H == 1:
if g % 2 == 0: #if g even
B = 0 #setting binary bit to 0 or 1 depending if r g b is odd or even
H = H + 1 #adding to pixel count
Key = Key + 1 #adding to key count
if Count == Key:
decoded = decoded + str(A)#add the extracted binary translation to the final decoded string
decoded = decoded + str(B)
return decoded
else:
B = 1 #setting binary bit to 0 or 1 depending if r g b is odd or even
H = H + 1 #adding to pixel count
Key = Key + 1 #adding to key count
if Count == Key:
decoded = decoded + str(A)#add the extracted binary translation to the final decoded string
decoded = decoded + str(B)
return decoded
elif H == 2:
if b % 2 == 0: #if b even
C = 0 #setting binary bit to 0 or 1 depending if r g b is odd or even
H = H + 1 #adding to pixel count
Key = Key + 1 #adding to key count
if Count == Key:
decoded = decoded + str(A)
decoded = decoded + str(B)#add the extracted binary translation to the final decoded string
decoded = decoded + str(C)
return decoded
else:
C = 1 #setting binary bit to 0 or 1 depending if r g b is odd or even
H = H + 1 #adding to pixel count
Key = Key + 1 #adding to key count
if Count == Key:
decoded = decoded + str(A)
decoded = decoded + str(B)#add the extracted binary translation to the final decoded string
decoded = decoded + str(C)
return decoded
H = 0
decoded = decoded + str(A)
decoded = decoded + str(B) #add the extracted binary translation to the final decoded string
decoded = decoded + str(C)
def process():
z = str("") #varible setup
word = input("Sentence to Encode (for numbers type them out for example one instead of 1)") #ask user for sentance to encode
word = word.split() #split into list for each word
for i in word: #for each word in the potential sentance inputted run the process for each word
temp = str(i)
z = z + ''.join(format(ord(x), 'b') for x in temp) #turn each char into binary and ammend
#print(z) code to debug correct binary output value
return(z)
def bin_string(binary): #https://stackoverflow.com/questions/29102121/how-to-convert-a-word-in-string-to-binary
bingen = (binary[i:i+7] for i in range(0, len(binary), 7))
return ''.join(chr(eval('0b'+n)) for n in bingen)
def Start():
print("""
### ### # #
# # # # #
# ## # ### ## # ### # #### ### # ## ### ## # # ## ### # ## # ## # #
# # # # # # # # # ### # # # ## # # # # # ## # # ## # ## # # #
# # # # #### ## ##### # # ##### # # # # ## # #### ## # # # # ##
# # # # # # # # # # # # # # # # # # # # # # ## # # ## #
### # # #### ### ### ### ## ### # # ### ### # #### # # # #
# # # # # # #
### ### # ###
--------------------------------------------by peel1--------------------------------------------------------------
""") #banner
idiot = False
while idiot == False:
choice = input("Input 0 for encoding and 1 for decoding") #user choice encoding or decoding
if choice == str(0):
encoded = process() #get encoded text
encrypt(encoded) #encode image
elif choice == str(1):
Count = int(input("Input the key provided when encoding")) #get key for decoding
decoded = decrypt(Count) #decode image
print("The text is: {}".format(bin_string(decoded))) #show user the decoded text
else:
idiot = True #if 0 or 1 are not inputed
print("Invalid input restart program") #if 0 or 1 are not inputed
time.sleep(2.5)
exit()
#encoded = process()
#Count = encrypt(encoded)
#decoded = decrypt(Count) #Faster debug alternative running method
#print("Decoded {}".format(decoded))
#print("the text is {}".format(bin_string(decoded)))
Start() #script start