-
Notifications
You must be signed in to change notification settings - Fork 0
/
steganography.py
91 lines (69 loc) · 2.43 KB
/
steganography.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
import traceback
import csv
from PIL import Image, ImageDraw
from random import randint
# CUSTOM EXCEPTION
class MessageIsTooLong(Exception):
def __init__(self, text):
self.txt = text
# WRITE MESSAGE TO IMAGE
def encrypt(path_to_image, path_to_keys):
keys = list()
image = Image.open(path_to_image)
draw = ImageDraw.Draw(image)
pixels = image.load()
width = image.size[0]
height = image.size[1]
message = input("Input message: ")
if len(message) < width * height:
for letter_code in ([ord(letter) for letter in message]):
key = (randint(1, width - 10), randint(1, height - 10))
r, g = pixels[key][0:2]
draw.point(key, (r, g, letter_code))
keys.append(key)
new_file_full_name = input("Input new file full name (path): ")
image.save(new_file_full_name, "png")
write_keys_to_file(path_to_keys, keys)
else:
raise MessageIsTooLong("Message is too long for this image!")
# GET MESSAGE FROM IMAGE
def decrypt(path_to_image, path_to_keys):
keys = read_keys_from_file(path_to_keys)
image = Image.open(path_to_image)
pixels = image.load()
result = list()
for key in keys:
result.append(pixels[key][2])
return ''.join([chr(symbol) for symbol in result])
# WRITE KEYS TO FILE
def write_keys_to_file(path, keys):
with open(path, "w") as file:
for key in keys:
file.write(str(key) + '\n')
# READ KEYS FROM FILE
def read_keys_from_file(path):
keys = list()
with open(path, "r") as file:
reader = csv.reader(file)
for row in reader:
pixel_coordinates = list()
for string in row:
pixel_coordinate = int("".join([char for char in string if char.isdigit()]))
pixel_coordinates.append(pixel_coordinate)
keys.append(tuple(pixel_coordinates))
return keys
if __name__ == '__main__':
try:
print("STEGANOGRAPHY:")
image_path = input("Path to image: ")
keys_path = input("Path to keys: ")
mode = input("\nSelect mode: \n1 - Encrypt\n2 - Decrypt\n")
if mode == "1":
encrypt(image_path, keys_path)
elif mode == "2":
message = decrypt(image_path, keys_path)
print("Message is: '" + message + "'")
else:
print("Invalid mode selected!")
except Exception as ex:
print(traceback.format_exc())