-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathPySteg.py
139 lines (112 loc) · 4.1 KB
/
PySteg.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
from PIL import ImageTk, Image
import math
import os
def encodeMessage(image, msg, fileName):
msg = "." + msg
width, height = image.size
if (width * height) < len(msg):
raise ValueError
pixels = image.load()
for i in range(0, len(msg)):
encodedChar = str(ord(msg[i]))
row = math.ceil(i/float(width))
column = i%width
pixel = pixels[row-1, column-1]
newPixel = list(pixel)
# Manipulating Pixels
newPixel[0] = str(newPixel[0])
newPixel[1] = str(newPixel[1])
newPixel[2] = str(newPixel[2])
if ord(msg[i]) >= 100:
r = list(newPixel[0])
r[-1] = encodedChar[0]
newPixel[0] = ''.join(r)
r = list(newPixel[1])
r[-1] = encodedChar[1]
newPixel[1] = ''.join(r)
r = list(newPixel[2])
r[-1] = encodedChar[2]
newPixel[2] = ''.join(r)
elif ord(msg[i]) >= 10:
r = list(newPixel[0])
r[-1] = '0'
newPixel[0] = ''.join(r)
r = list(newPixel[1])
r[-1] = encodedChar[0]
newPixel[1] = ''.join(r)
r = list(newPixel[2])
r[-1] = encodedChar[1]
newPixel[2] = ''.join(r)
else:
r = list(newPixel[0])
r[-1] = '0'
newPixel[0] = ''.join(r)
r = list(newPixel[1])
r[-1] = '0'
newPixel[1] = ''.join(r)
r = list(newPixel[2])
r[-1] = encodedChar[0]
newPixel[2] = ''.join(r)
newPixel[0] = int(newPixel[0])
newPixel[1] = int(newPixel[1])
newPixel[2] = int(newPixel[2])
pixels[row-1, column-1] = tuple(newPixel)
row = math.ceil(len(msg)/float(width))
column = len(msg)%width
pixel = pixels[row, column]
newPixel = list(pixel)
newPixel[0] = str(newPixel[0])
newPixel[1] = str(newPixel[1])
newPixel[2] = str(newPixel[2])
r = list(newPixel[0])
r[-1] = '1'
newPixel[0] = ''.join(r)
r = list(newPixel[1])
r[-1] = '2'
newPixel[1] = ''.join(r)
r = list(newPixel[2])
r[-1] = '7'
newPixel[2] = ''.join(r)
newPixel[0] = int(newPixel[0])
newPixel[1] = int(newPixel[1])
newPixel[2] = int(newPixel[2])
pixels[row-1, column-1] = tuple(newPixel)
image.save("./export/" + fileName)
def decodeMessage(image):
pixels = image.load()
encodedText = ""
for w in range(0, image.size[0]):
for h in range(0, image.size[1]):
pixel = pixels[w,h]
encodedChar = int(str(pixel[0])[-1]) * 100 + int(str(pixel[1])[-1]) * 10 + int(str(pixel[2])[-1])
if(encodedChar == 127):
return encodedText
else:
encodedText+=chr(encodedChar)
return encodedText
if __name__== "__main__":
while True:
choice = input("Decode or encrypt? (d/e): ")
while not(choice == 'd' or choice == 'e'):
print("\n[ERROR] Invalid choice, please input 'd' or 'e'!")
choice = input("\nDecode or encrypt? (d/e): ")
print()
if(choice == 'd'):
for subdir, dirs, files in os.walk("./decrypt"):
for file in files:
filepath = subdir + os.sep + file
if filepath.endswith(".png"):
print("Decoded message from [" + file + "]: " + decodeMessage(Image.open("./decrypt/" + file)))
print("\nFinished Decoding images.")
else:
msg = input('Enter message to encode: ')
for subdir, dirs, files in os.walk("./encrypt"):
for file in files:
filepath = subdir + os.sep + file
if filepath.endswith(".png"):
try:
encodeMessage(Image.open("./encrypt/" + file), msg, file)
print("Encoded message into [" + file + "]")
except ValueError:
print("Message too long to encode in [" + file + "]")
print("\nFinished Encoding images.")