-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
315 lines (247 loc) · 8.75 KB
/
main.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
# Python program implementing Image Steganography
# PIL module is used to extract
# pixels of image and modify it
import streamlit as st
from PIL import Image
from playFair import playfair_encrypt
from playFair import playfair_decrypt
# Convert encoding data into 8-bit binary
# form using ASCII value of characters
import streamlit as st
#modify this code into a streamlit app
st.set_page_config(page_title="cipherShroud")
st.markdown("<h1 style='text-align: center; color: white;'>cipherShroud</h1>",
unsafe_allow_html=True)
def encode_img(img1, img2):
img1_pixels = img1.load()
img2_pixels = img2.load()
cover_up = Image.new('RGB', (img1.width, img1.height), 'black')
cover_up_pixels = cover_up.load()
for x in range(0, cover_up.width):
for y in range(0, cover_up.height):
br, bg, bb = img1_pixels[x, y]
ar, ag, ab = img2_pixels[x, y]
# first 4 bytes from cover up (msb) + first 4 bytes of secret
cr = int(f'{br:08b}'[:4] + f'{ar:08b}'[:4], 2)
cg = int(f'{bg:08b}'[:4] + f'{ag:08b}'[:4], 2)
cb = int(f'{bb:08b}'[:4] + f'{ab:08b}'[:4], 2)
cover_up_pixels[x, y] = (cr, cg, cb)
new_img_name = st.text_input("Enter name of stego image(with extension) :")
cover_up.save(new_img_name, str(new_img_name.split(".")[1].upper()))
return cover_up, new_img_name
def decode_img(cover_up):
cover_up_pixels = cover_up.load()
secret = Image.new('RGB', (cover_up.width, cover_up.height), 'black')
secret_pixels = secret.load()
for x in range(0, secret.width):
for y in range(0, secret.height):
r, g, b = cover_up_pixels[x, y]
sr = int(f'{r:08b}'[4:] + '0000', 2)
sg = int(f'{g:08b}'[4:] + '0000', 2)
sb = int(f'{b:08b}'[4:] + '0000', 2)
secret_pixels[x, y] = (sr, sg, sb)
return secret
def genData(data):
# list of binary codes
# of given data
newd = []
for i in data:
newd.append(format(ord(i), '08b'))
return newd
# Pixels are modified according to the
# 8-bit binary data and finally returned
def modPix(pix, data):
datalist = genData(data)
lendata = len(datalist)
imdata = iter(pix)
for i in range(lendata):
# Extracting 3 pixels at a time
pix = [
value for value in imdata.__next__()[:3] + imdata.__next__()[:3] +
imdata.__next__()[:3]
]
# Pixel value should be made
# odd for 1 and even for 0
for j in range(0, 8):
if (datalist[i][j] == '0' and pix[j] % 2 != 0):
pix[j] -= 1
elif (datalist[i][j] == '1' and pix[j] % 2 == 0):
if (pix[j] != 0):
pix[j] -= 1
else:
pix[j] += 1
# pix[j] -= 1
# Eighth pixel of every set tells
# whether to stop ot read further.
# 0 means keep reading; 1 means thec
# message is over.
if (i == lendata - 1):
if (pix[-1] % 2 == 0):
if (pix[-1] != 0):
pix[-1] -= 1
else:
pix[-1] += 1
else:
if (pix[-1] % 2 != 0):
pix[-1] -= 1
pix = tuple(pix)
yield pix[0:3]
yield pix[3:6]
yield pix[6:9]
def encode_enc(newimg, data):
w = newimg.size[0]
(x, y) = (0, 0)
for pixel in modPix(newimg.getdata(), data):
# Putting modified pixels in the new image
newimg.putpixel((x, y), pixel)
if (x == w - 1):
x = 0
y += 1
else:
x += 1
# Encode data into image
def encode(image):
try:
data = st.text_input("Enter the text to be hidden"
) # Input the text to be hidden from the user
#img = input("Enter image name(with extension) : ")
#image = Image.open(img, 'r')
#data = input("Enter data to be encoded : ")
# if (len(data) == 0):
# raise ValueError('Data is empty')
newimg = image.copy()
key = 'stegano'
ciphertext = playfair_encrypt(data, key)
#ciphertext=ciphertext.encode()
encode_enc(newimg, ciphertext)
new_img_name = st.text_input("Enter name of stego image(with extension) :")
#print(str(new_img_name.split(".")[1].upper()))
newimg.save(new_img_name, 'PNG')
return newimg, new_img_name
except:
# Prevent the error from propagating into your Streamlit app.
pass
# Decode the data in the image
def decode(image):
# data = st.text_input("Enter the text to be hidden")
# img = input("Enter image name(with extension) : ")
# image = Image.open(img, 'r')
data = ''
imgdata = iter(image.getdata())
while (True):
pixels = [
value for value in imgdata.__next__()[:3] + imgdata.__next__()[:3] +
imgdata.__next__()[:3]
]
# string of binary data
binstr = ''
for i in pixels[:8]:
if (i % 2 == 0):
binstr += '0'
else:
binstr += '1'
data += chr(int(binstr, 2))
key = 'stegano'
data1 = playfair_decrypt(data, key)
if (pixels[-1] % 2 != 0):
return data1
footer = """<style>
a:link , a:visited{
color: whites;
background-color: transparent;
text-decoration: underline;
}
a:hover, a:active {
color: white;
background-color: transparent;
text-decoration: underline;
}
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: black;
color: white;
text-align: center;
}
</style>
<div class="footer">
<p>Developed by <a style='display: block; text-align: center;' href="https://github.com/AyushPathak3011/cipherShroud" target="_blank">Team cipherShroud</a></p>
</div>
"""
# Main Function
def main():
# Streamlit UI
try:
option = st.selectbox(
'Choose an action',
('Encode', 'Decode')) # Ask the user to choose either encode or decode
if option == 'Encode':
uploaded_file = st.file_uploader(
"Choose a cover image",
key='cover_image') # Input a file from the file browser from the user
if uploaded_file is not None:
image = Image.open(uploaded_file)
option = st.selectbox('Choose Stego type', ('Hide Image', 'Hide Text'))
if option == 'Hide Image':
#logic for hiding image in image
# Ask the user to choose either encode or decode
st.image(image, caption='Uploaded Cover Image.', use_column_width=True)
uploaded_file = st.file_uploader(
"Choose an image to hide", key='hide_image'
) # Input a file from the file browser from the user
if uploaded_file is not None:
imageHide = Image.open(uploaded_file)
st.image(imageHide, caption='Image to hide.', use_column_width=True)
stego_img, new_img_name = encode_img(image, imageHide)
if st.button('Encode'):
st.image(stego_img, caption='Stego Image.', use_column_width=True)
if new_img_name:
with open(new_img_name, "rb") as file:
btn = st.download_button(label="Download Stego image",
data=file,
file_name=new_img_name,
mime="image/png")
elif option == 'Hide Text':
#logic for hiding text in image
st.image(image, caption='Uploaded Cover Image.', use_column_width=True)
newimg, new_img_name = encode(image)
if st.button('Encode'):
st.image(newimg, caption='New Stego Image.',
use_column_width=True) # Display the new stego image
if new_img_name:
with open(new_img_name, "rb") as file:
btn = st.download_button(label="Download Stego image",
data=file,
file_name=new_img_name,
mime="image/png")
elif option == 'Decode':
uploaded_file = st.file_uploader(
"Choose the Stego Image", key='image_to_decode'
) # Input a file from the file browser from the user
image = None # Initialize image variable
if uploaded_file is not None:
image = Image.open(uploaded_file)
if image is not None:
st.image(image, caption='Image to be decoded.', use_column_width=True)
else:
st.write('No image provided for decoding.')
option = st.selectbox('Choose Stego type',
('Extract Image', 'Extract Text'))
if option == 'Extract Text':
if st.button(
'Decode') and image is not None: # Check if image is not None
decoded_data = decode(image)
st.text('Decoded Text: ' + decoded_data) # Display the decoded text
elif option == 'Extract Image':
if st.button('Decode') and image is not None:
decoded_data = decode_img(image)
st.image(decoded_data, caption='Decoded Image.', use_column_width=True)
st.markdown(footer, unsafe_allow_html=True)
except:
pass
# Driver Code
if __name__ == '__main__':
# Calling main function
main()