-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhide.py
executable file
·175 lines (132 loc) · 3.52 KB
/
hide.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
from PIL import Image
from bitarray import bitarray
import struct
import sys
class textColors:
ERROR = '\033[1;31;48m'
SUCCES = '\033[1;32;48m'
END = '\033[1;37;48m'
def encode(inFile):
try:
inImage = Image.open(inFile)
except:
print(textColors.ERROR+"No such file: "+inFile+'!'+textColors.END)
exit()
maxMessSize = inImage.size[0]*inImage.size[1]*3/8
if maxMessSize > 65535:
maxMessSize = 65535
print("Max character capacity of "+inFile+" image: " + str(maxMessSize))
m = input("Enter message to hide: ")
if len(m) > 65535:
print(textColors.ERROR+"Message is to long!"+textColors.END)
exit()
if maxMessSize < len(m):
print(textColors.ERROR+"Message is to long!"+textColors.END)
exit()
mb = bitarray()
mb.fromstring(m)
mbSize = mb.length()
c = 0 # counter
oldPixels = inImage.load()
newImage = Image.new( 'RGB', (inImage.size[0], inImage.size[1]), "black")
pixels = newImage.load()
for i in range(newImage.size[0]): # for every col
for j in range(newImage.size[1]): # For every row
r,g,b = oldPixels[i,j]
if c < mbSize:
if mb[c] == 1:
r = r | 1
else:
r = r & 254
c += 1
if c < mbSize:
if mb[c] == 1:
g = g | 1
else:
g = g & 254
c += 1
if c < mbSize:
if mb[c] == 1:
b = b | 1
else:
b = b & 254
c += 1
pixels[i,j] = (r,g,b)
newImage.save("r.png")
try:
size = struct.pack('H', len(m)).decode("utf-8")
except:
print(textColors.ERROR+"Something went wrong! r.png does not contain hidden message"+textColors.END)
exit()
try:
f = open("r.png", "a+")
except:
print(textColors.ERROR+"Something went wrong! r.png does not contain hidden message"+textColors.END)
exit()
f.write(size)
f.close()
print(textColors.SUCCES+"Succes! Message encrypted in file r.png"+textColors.END)
def decode(inFile):
try:
f = open(inFile, "rb")
except:
print(textColors.ERROR+"No such file: "+inFile+'!'+textColors.END)
exit()
fstr = f.read()
strLen = fstr[-2:]
length = struct.unpack('H', strLen)
length = int(length[0])
f.close()
length = length*8
try:
inImage = Image.open(inFile)
except:
print(textColors.ERROR+"No such file: "+inFile+'!'+textColors.END)
exit()
pixels = inImage.load()
bc = 0 # bit counter
mb = bitarray() # array for string
for i in range(inImage.size[0]): # for every col
for j in range(inImage.size[1]): # For every row
r,g,b = pixels[i,j]
if bc < length:
mb.append(r & 1)
bc+=1
else:
break
if bc < length:
mb.append(g & 1)
bc+=1
else:
break
if bc < length:
mb.append(b & 1)
bc+=1
else:
break
decodedMessage = mb.tostring()
print(textColors.SUCCES+"Succes! Decoded message:")
print(decodedMessage+textColors.END)
def main():
try:
operation = sys.argv[1]
except:
print(textColors.ERROR+"Incorrect input!"+textColors.END)
print("Usage: python3 hide.py -e(to encode)/-d(to decode) input_file")
exit()
try:
inFile = sys.argv[2]
except:
print(textColors.ERROR+"Incorrect input!"+textColors.END)
print("Usage: python3 hide.py -e(to encode)/-d(to decode) input_file")
exit()
if operation == '-e':
encode(inFile)
elif operation == '-d':
decode(inFile)
else:
print(textColors.ERROR+"Incorrect input!"+textColors.END)
print("Usage: python3 hide.py -e(to encode)/-d(to decode) input_file")
exit()
if __name__ == "__main__":
main()