-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteg.py
281 lines (250 loc) · 6.38 KB
/
steg.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
#pip install progressbar2
import sys
import time
import progressbar
def main():
widgets=[
' [', progressbar.Timer(), '] ',
progressbar.Bar(),
' (', progressbar.ETA(), ') ',
]
#message to binary
def string_to_bin(mess):
l = []
for i in mess:
b = bin(ord(i))[2:]
if len(bin(ord(i))[2:]) < 8:
for i in range(8-len(bin(ord(i))[2:])):
b ='0' + b
for i in range(8):
l.append(int(b[i]))
return l
#add len message to output file
def len_mess_e(len_m, l_out):
n = hex(len_m)[2:]
for i in range(8-len(n)):
n = '0' + n
l = []
for i in range(0,len(n),2):
s = '0x' + n[i] + n[i+1]
l.append(int(s,16))
for i in range(4):
l_out.append(chr(l[i]))
return l_out
#check_parity
def check_parity_encode(a, b, mess):
count = 0
a = bin(ord(a))[2:]
b = bin(ord(b))[2:]
if len(a) < 8:
for i in range(8-len(a)):
a = '0' + a
if len(b) < 8:
for i in range(8-len(b)):
b ='0' + b
a = a+b
lsb = a[-1:]
for i in a[:-1]:
if int(i) == 1:
count += 1
if count % 2 == 0: # even parity
if mess == 1: # message embed 1
lsb = 1
else: # message embed 0
lsb = 0
else: # odd parity
if mess == 0: # message embed 0
lsb = 1
else: # message embed 1
lsb = 0
a = a[:-1] + str(lsb)
return chr(int("0b"+a[0:8],2)%256), chr(int("0b"+a[8:],2)%256)
def add_Pin(pin_e,l_out):
h = hex(pin_e)[2:]
for i in range(6-len(h)):
h = "0"+h
l_out.append(chr(int("0x"+h[:2],16)))
l_out.append(chr(int("0x"+h[2:4],16)))
l_out.append(chr(int("0x"+h[4:],16)))
return l_out
#encode
def encode(input_f, output_f, message_f):
# read sample
f = open(input_f, 'rb')
l_in = f.read()
# read message
file_mess = open(message_f,'rb')
message = file_mess.read()
m = string_to_bin(message)
# Use pin_e
pin_e = raw_input("Enter the PIN: ")
while len(pin_e) > 6 or not pin_e.isdigit():
print "PIN has a maximum length of 6 characters and only include digits"
pin_e = raw_input("Enter the PIN: ")
pin_e = int(pin_e)
if pin_e <= 64:
pin_e += 64
if (len(l_in) - pin_e-1) < len(m):
a = (len(l_in)-pin_e-1)/16
print "Message too big. You only hiding %d words."%a
else:
l_out = []
print "\nRead file "+input_f+" and embed message.\n"
#copy 64 bytes header
for i in range(pin_e):
l_out.append(l_in[i])
#add PIN
lout = add_Pin(pin_e,l_out)
#add length message
l_out = len_mess_e(len(message), l_out)
#add message
for i in progressbar.progressbar(range(pin_e+7, len(l_in), 2)):
if (i-pin_e-7)/2 < len(m):
a, b = check_parity_encode(l_in[i], l_in[i+1], m[(i-pin_e-7)/2])
l_out.append(a)
l_out.append(b)
else:
try:
l_out.append(l_in[i])
l_out.append(l_in[i+1])
except:
l_out.append(l_in[i])
print "\nMake file " + output_f + "\n"
#write to output file
o = open(output_f, 'wb')
for i in progressbar.progressbar(range(len(l_out))):
o.write(l_out[i])
print "\nSuccess!!!"
#binary to string
def bin_to_string(l):
a = []
for i in range(0,len(l),8):
s=''
for j in range(i, i+8):
s+=str(l[j])
s = '0b' + s
s = int(s,2)
a.append(s)
st = ''
for i in range(len(a)):
st+=chr(a[i])
return st
#read len message to input file
def len_mess_d(l_in, pin_d):
l = []
for i in range(4):
l.append(ord(l_in[pin_d+i]))
s = '0x'
for i in range(len(l)):
if len(hex(l[i])[2:])>1:
s+=str(hex(l[i])[2:])
else:
s+="0" + str(hex(l[i])[2:])
return int(s,16)
#check PIN
def check_PIN(l_in, pin_d):
l = []
for i in range(3):
l.append(ord(l_in[pin_d+i]))
s = '0x'
for i in range(len(l)):
if len(hex(l[i])[2:])>1:
s+=str(hex(l[i])[2:])
else:
s+="0" + str(hex(l[i])[2:])
pin = int(s,16)
if pin != pin_d:
print "The PIN you input maybe wrong. Try another PIN."
exit(1)
def check_parity_decode(a, b):
count = 0
a = bin(ord(a))[2:]
b = bin(ord(b))[2:]
if len(a) < 8:
for i in range(8-len(a)):
a = '0' + a
if len(b) < 8:
for i in range(8-len(b)):
b ='0' + b
a = a+b
for i in a:
if int(i) == 1:
count += 1
if count % 2 == 0:
return 0
else:
return 1
#decode
def decode(input_f,output_f):
# read sample
f = open(input_f,'rb')
l_in = f.read()
l_out = []
#Use pin_e
pin_d = raw_input("Enter the PIN: ")
while len(pin_d) > 6 or not pin_d.isdigit():
print "PIN has a maximum length of 6 characters and only include digits"
pin_d = raw_input("Enter the PIN: ")
pin_d = int(pin_d)
if pin_d <= 64:
pin_d += 64
check_PIN(l_in,pin_d)
len_mess = len_mess_d(l_in, pin_d+3) #check length message
print "\nProcessing...\n"
for i in progressbar.progressbar(range(pin_d+7, pin_d+7+len_mess*16,2)):
a = check_parity_decode(l_in[i], l_in[i+1])
l_out.append(a)
time.sleep(0.0002)
#write to ouput file
st = bin_to_string(l_out)
o = open(output_f,'wb')
o.write(st)
print "\nSuccess!!!"
#Usage:
usage = '''Tool steganography to hiding message into audio using Parity method
Usage:
Encode:
-e: Encode
-i: Input file
-m: Message file
-o: Output file
Example: python steg.py -e -i Example1.wav -m message.txt -o Example2.wav
Decode:
-d: Decode
-i: Input file
-o: Message output file
Example: python steg.py -e -i Example1.wav -o message.txt
------------------------------Unicorn TEAM------------------------------
'''
if len(sys.argv) > 1:
if sys.argv[1] == '-e':
input_f = ''
message_f = ''
output_f = ''
for i in range(2, len(sys.argv)):
if sys.argv[i] == '-i':
input_f = sys.argv[i+1]
elif sys.argv[i] == '-m':
message_f = sys.argv[i+1]
elif sys.argv[i] =='-o':
output_f = sys.argv[i+1]
if (input_f == '' or message_f == '' or output_f == ''):
print usage
else:
encode(input_f, output_f,message_f)
elif sys.argv[1] == '-d':
input_f = ''
output_f = ''
for i in range(2, len(sys.argv)):
if sys.argv[i] == '-i':
input_f = sys.argv[i+1]
elif sys.argv[i] =='-o':
output_f = sys.argv[i+1]
if (input_f == '' or output_f == ''):
print usage
else:
decode(input_f, output_f)
else:
print usage
if __name__ == '__main__':
main()