-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret.py
208 lines (176 loc) · 5.47 KB
/
secret.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
"""
Steganography
A python program that hides information (text messages) in png files
Based on DrapsTV's "Steganography Tutorial - Hiding Text inside an Image" https://www.youtube.com/watch?v=q3eOOMx5qoo
Loads an image and look at each pixel's hex value
If the pixel's blue channel falls in the 0-5 range then 1 bit of info is stored
Ends the stream with a delimiter of fifteen 1's and one 0.
"""
from PIL import Image
import binascii
import optparse
def rgb2hex(r, g, b):
"""
Turn rgb color values to hex color values
:param r (int): red chanel value
:param g (int): green chanel value
:param b (int): blue chanel value
:return (string): a hex value
"""
return '#{:02x}{:02x}{:02x}'.format(r, g, b)
def hex2rgb(hexcode):
"""
Turn hex color values to rgb color values
:param hexcode (string): hex value
:return (tuple): r, g, b value
"""
assert isinstance(hexcode, str)
assert len(hexcode) == 7
r = int(hexcode[1:3], 16)
g = int(hexcode[3:5], 16)
b = int(hexcode[5:7], 16)
return r, g, b
def str2bin(message):
"""
Turn a message into binary omit first two digits
:param message: This should be a str encoded in utf-8
:return: binary string
"""
message_bytes = bytes(message, "utf-8")
binary = bin(int(binascii.hexlify(message_bytes), 16))
return binary[2:]
def bin2str(binary):
"""
Turn a binary into string
:param binary:
:return:
"""
message_bytes = binascii.unhexlify('%x' % (int(binary, 2)))
return str(message_bytes, encoding="utf-8")
def encodeblue(hexcode, digit):
"""
:param hexcode:
:param digit:
:return:
"""
if hexcode[-1] in ('0', '1', '2', '3', '4', '5'):
hexcode = hexcode[:-1] + digit
return hexcode
else:
return None
def encodegreen(hexcode, digit):
"""
:param hexcode:
:param digit:
:return:
"""
if hexcode[-3] in ('0', '1', '2', '3', '4', '5'):
hexcode = hexcode[:-3] + digit + hexcode[5:]
return hexcode
else:
return None
def decodeblue(hexcode):
"""
:param hexcode:
:return:
"""
if hexcode[-1] in ('0', '1'):
return hexcode[-1]
else:
return None
def decodegreen(hexcode):
"""
:param hexcode:
:return:
"""
if hexcode[-3] in ('0', '1'):
return hexcode[-3]
else:
return None
def hide(filename, message):
"""
Hide the message in the png image
:param filename (png): the png file that's been used as the medium
:param message (string): the secret message you are trying to hide
:return:
"""
img = Image.open(filename)
binary = str2bin(message) + '1111111111111110'
if img.mode in 'RGBA':
img = img.convert('RGBA')
pixels = img.getdata()
new_pixels = []
digit = 0
count = 0
for item in pixels:
if digit < len(binary) and count % 2 == 0:
new_pix = encodeblue(rgb2hex(item[0], item[1], item[2]), binary[digit])
if new_pix is None:
new_pixels.append(item)
else:
r, g, b = hex2rgb(new_pix)
new_pixels.append((r, g, b, 255))
digit += 1
elif digit < len(binary) and count % 2 != 0:
new_pix = encodegreen(rgb2hex(item[0], item[1], item[2]), binary[digit])
if new_pix is None:
new_pixels.append(item)
else:
r, g, b = hex2rgb(new_pix)
new_pixels.append((r, g, b, 255))
digit += 1
else:
new_pixels.append(item)
count += 1
img.putdata(tuple(new_pixels))
img.save(filename, 'PNG')
return "Completed!"
return "Incorrect Image mode detect, couldn't hide message"
def retr(filename):
"""
retrieve the message from the png image
:param filename: the png file that contains the secret message
:return: the secret message
"""
img = Image.open(filename)
binary = ''
count = 0
if img.mode in 'RGBA':
img = img.convert('RGBA')
pixels = img.getdata()
for item in pixels:
if count % 2 == 0:
digit = decodeblue(rgb2hex(item[0], item[1], item[2]))
else:
digit = decodegreen(rgb2hex(item[0], item[1], item[2]))
count += 1
if digit is None:
pass
else:
binary = binary + digit
if binary[-16:] == '1111111111111110':
print("success")
return bin2str(binary[:-16])
return bin2str(binary)
return "Incorrect Image mode detect, couldn't retrieve message"
def main():
"""
parameter handling
"""
parser = optparse.OptionParser('usage %png '
'-e/-d <target file>')
parser.add_option('-e', dest='hide', type='string',
help='target picture parse to hide text')
parser.add_option('-d', dest='retr', type='string',
help='target picture parse to retrieve text')
(options, args) = parser.parse_args()
if options.hide is not None:
text = input("Enter a message to hide: ")
print(hide(options.hide, text))
elif options.retr is not None:
print(retr(options.retr))
else:
print(parser.usage)
exit(0)
if __name__ == "__main__":
main()