-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·382 lines (300 loc) · 11.5 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
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
import sys
import os
import imghdr
import time
from getopt import getopt
from steganography import Steganography
ACCEPTED_FORMATS = ['jpeg', 'png']
s = Steganography()
def main():
action = None
img_path = None
message = None
password = None
# read args
if len(sys.argv) > 1:
# action in args
if sys.argv[1].lower() in ['encode', 'decode']:
action = sys.argv[1].lower()
opts, _ = getopt(sys.argv[2:], "i:f:m:p:b:e:")
for opt in opts:
if opt[0] == '-i':
img_path = opt[1]
if not is_valid_image(img_path):
exit()
elif opt[0] == '-f':
path = opt[1]
if not is_valid_textfile(path):
exit()
with open(path, 'r') as file:
message = file.read()
elif opt[0] == '-m':
message = opt[1]
elif opt[0] == '-p':
password = opt[1]
elif opt[0] == '-b':
bits = opt[1]
if bits.isnumeric() and int(bits) <= 8:
s.BITS = int(bits)
else:
print('\033[0;31mInvalid bits number.\033[0m')
exit()
elif opt[0] == '-e':
padding = opt[1]
if padding.isnumeric():
s.EOF_LENGTH = int(padding)
else:
print('\033[0;31mInvalid padding length.\033[0m')
exit()
# no action in args
else:
opts, _ = getopt(sys.argv[1:], "b:e:")
for opt in opts:
if opt[0] == '-b':
bits = opt[1]
if bits.isnumeric() and int(bits) <= 8:
s.BITS = int(bits)
else:
print('\033[0;31mInvalid bits number.\033[0m')
exit()
elif opt[0] == '-e':
padding = opt[1]
if padding.isnumeric():
s.EOF_LENGTH = int(padding)
else:
print('\033[0;31mInvalid padding length.\033[0m')
exit()
if not bits and not padding:
print_help()
exit()
print_title()
if not action:
action = choose_action()
# encode
if action == 'encode':
if not img_path:
img_path = choose_image(ACCEPTED_FORMATS)
if not message:
message = choose_message()
if not password:
password = choose_password('Do tou want to encrypt the message?')
print('\n\033[1;2mEncoding...\033[0m')
outfile_name = s.encode(img_path, message, password)
print('\033[1;32mImage successfully encoded!\033[0m')
print(f'\nThe encoded image is: \033[1m{outfile_name}\033[0m\n')
# decode
elif action == 'decode':
if not img_path:
img_path = choose_image(['png'])
if not password:
password = choose_password('Was the message encrypted?')
print('\n\033[1;2mDecoding...\033[0m')
decoded_message = s.decode(img_path, password)
print('\033[1;32mImage successfully decoded!\033[0m')
print(f'\n\033[1mThe message is:\033[0m')
print(decoded_message, '\n')
def choose_action() -> str:
"""Asks the action (encode, decode) to the user.
Returns:
action (str): the action chosen by the user.
"""
while True:
print("""
\033[1mChoose action:\033[0m
[1]\033[0;3m Encode\033[0m
[2]\033[0;3m Decode\033[0m
[3]\033[0;3m Settings\033[0m
[4]\033[0;3m Help\033[0m
[0]\033[0;3m Exit\033[0m
""")
a = None
while a not in ['1', '2', '3', '4', '0']:
a = input('> ')
if a == '0':
exit()
elif a == '1':
return 'encode'
elif a == '2':
return 'decode'
elif a == '3':
settings()
elif a == '4':
print_help()
exit()
def settings():
"""Displays settings."""
while True:
print("""
\033[1mSettings:\033[0m
[1]\033[0;3m Change number of bits\033[0m
[2]\033[0;3m Change padding size\033[0m
[0]\033[0;3m Exit\033[0m
""")
s = None
while s not in ['1', '2', '0']:
s = input('> ')
if s == '0':
exit()
elif s == '1':
choose_bits()
break
elif s == '2':
choose_padding()
break
def choose_bits():
"""Asks the user for the number of bits reserved to the hidden message and changes it."""
while True:
n = input("\n\033[1mNumber of bits reserved to the hidden message: \033[0;2m(default is 2; maximum 8; 0 to exit)\033[0m ")
if n == '0':
exit()
elif n.isnumeric() and int(n) <= 8:
s.BITS = int(n)
print('\033[0;32mNumber of bits reserved to the hidden message updated successfully!\033[0m')
break
def choose_padding():
"""Asks the user for the length of padding and changes it."""
while True:
n = input("\n\033[1mNumber zeros in message padding: \033[0;2m(default is 16; 0 to exit)\033[0m ")
if n == '0':
exit()
elif n.isnumeric():
s.EOF_LENGTH = int(n)
print('\033[0;32mNumber zeros in message padding updated successfully!\033[0m')
break
def choose_image(accepted_formats: list) -> str:
"""Asks the image path to the user.
Args:
accepted_formats (list): a list with accepted image formats.
Returns:
img_path (str): the path of the chosen image.
"""
while True:
path = input('\n\033[1mInsert the path of the image: \033[0;2m(0 to exit)\033[0m ')
if path == '0':
exit()
elif is_valid_image(path, accepted_formats):
time.sleep(0.5)
print('\033[0;32m✓ Valid image.\033[0m')
time.sleep(0.5)
break
return path
def is_valid_image(image_path: str, accepted_formats: list) -> bool:
"""Checks if the user given image path is valid.
Args:
image_path (str): the path of the user given image.
accepted_formats (list): a list with accepted image formats.
Returns:
is_valid (bool): if the image is valid.
"""
if not os.path.isfile(image_path):
print(f'\033[0;31mFile "{os.path.abspath(image_path)}" does not exist.\033[0m')
return False
elif imghdr.what(image_path) not in accepted_formats:
print(f'\033[0;31mFile "{image_path}" has an invalid format.\033[0m')
print(f'\033[0;31mAccepted formats:', ', '.join(accepted_formats), '\033[0m')
return False
return True
def choose_message() -> str:
"""Asks the message to the user.
Returns:
message (str): the message given by the user.
"""
while True:
print("""
\033[1mChoose message type:\033[0m
[1]\033[0;3m Text\033[0m
[2]\033[0;3m Textfile\033[0m
[0]\033[0;3m Exit\033[0m
""")
m = None
while m not in ['1', '2', '0']:
m = input('> ')
if m == '0':
exit()
elif m == '1':
text = input('\n\033[1mInsert the message: \033[0;2m(0 to exit)\033[0m ')
if text == '0':
exit()
return text
elif m == '2':
while True:
path = input('\n\033[1mInsert the path of the textfile: \033[0;2m(0 to exit)\033[0m ')
if path == '0':
exit()
elif is_valid_textfile(path):
time.sleep(0.5)
print('\033[0;32m✓ Valid textfile.\033[0m')
time.sleep(0.5)
break
with open(path, 'r') as file:
return file.read()
def is_valid_textfile(textfile_path: str) -> bool:
"""Chack if the user given textfile path is valid.
Args:
textfile_path (str): the path of the textfile.
Returns:
is_valid (bool): if the textfile path is valid.
"""
if not os.path.isfile(textfile_path):
print(f'\033[0;31mFile "{os.path.abspath(textfile_path)}" does not exist.\033[0m')
return False
elif not textfile_path.endswith('.txt'):
print(f'\033[0;31mFile "{textfile_path}" is not a .txt file.\033[0m')
return False
return True
def choose_password(message: str) -> str:
"""Asks the user for the encryption password.
Args:
message (str): the question asked by the function.
Returns:
password (str | None): the password given by the user.
"""
while True:
print(f"""
\033[1m{message}\033[0m
[1]\033[0;3m Yes\033[0m
[2]\033[0;3m No\033[0m
[0]\033[0;3m Exit\033[0m
""")
p = None
while p not in ['1', '2', '0']:
p = input('> ')
if p == '0':
exit()
elif p == '1':
password = input('\n\033[1mInsert the password: \033[0;2m(0 to exit)\033[0m ')
if password == '0':
exit()
return password
elif p == '2':
return None
def print_help():
"""Prints the help message."""
print(f"""
\033[1mSteganography\033[0m
This program allows you to hide messages inside images using the LSB algorithm which allows you to hide data in the least significat bits of the image.
For more info check the GitHub repo: https://github.com/francesco-dorati/steganograpy
\033[1mUsage:\033[0m
{sys.argv[0]} [encode [-i image] [-f textfile | -m message] [-p password]] [-b bits] [-e padding]
{sys.argv[0]} [decode [-i image] [-p password]] [-b bits] [-e padding]
\033[1mArgs:\033[0m
-i image Image path to be encoded/decoded.
-f textfile Textfile (.txt) to be hidden.
-m message The mesage to be hidden.
-p password The password to encrypt/decrypt the hidden message.
-b bits The number of image pixel bits reserved for the hidden message.
-e padding The end-of-message padding (number of zeroes).
""")
def print_title():
"""Prints the title."""
print("""\033[0;35m
.▄▄ · ▄▄▄▄▄▄▄▄ . ▄▄ • ▄▄▄· ▐ ▄ ▄▄ • ▄▄▄ ▄▄▄· ▄▄▄· ▄ .▄ ▄· ▄▌
▐█ ▀. •██ ▀▄.▀·▐█ ▀ ▪▐█ ▀█ •█▌▐█ ▄█▀▄ ▐█ ▀ ▪▀▄ █·▐█ ▀█ ▐█ ▄███▪▐█▐█▪██▌
▄▀▀▀█▄ ▐█.▪▐▀▀▪▄▄█ ▀█▄▄█▀▀█ ▐█▐▐▌▐█▌.▐▌▄█ ▀█▄▐▀▀▄ ▄█▀▀█ ██▀·██▀▀█▐█▌▐█▪
▐█▄▪▐█ ▐█▌·▐█▄▄▌▐█▄▪▐█▐█▪ ▐▌██▐█▌▐█▌.▐▌▐█▄▪▐█▐█•█▌▐█▪ ▐▌▐█▪·•██▌▐▀ ▐█▀·.
▀▀▀▀ ▀▀▀ ▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀█▄▀▪·▀▀▀▀ .▀ ▀ ▀ ▀ .▀ ▀▀▀ · ▀ • \033[0m
by \033[1mFrancesco Dorati\033[0m
\033[2mgithub.com: \033[1mfrancesco-dorati\033[0m
""")
if __name__ == '__main__':
main()