-
Notifications
You must be signed in to change notification settings - Fork 247
/
Copy pathLSB.py
266 lines (228 loc) · 8.35 KB
/
LSB.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
"""
@FileName: LSB.py
@Description: Implement LSB Algorithm
@Author: Ryuk
@CreateDate: 2020/12/06
@LastEditTime: 2020/12/06
@LastEditors: Please set LastEditors
@Version: v0.1
"""
import scipy.io.wavfile as wav
import numpy as np
np.random.seed(2020)
stop_mark = np.random.randint(0, 2, 128)
class LSBEmbedder:
def __init__(self, seed, rate=0.9, mode='single'):
self.rate = rate
self.seed = seed
self.mode = mode
self.stop_mark = stop_mark
self.channels = None
self.fs = None
self.left_signal = None
self.right_signal = None
self.wavsignal = None
self.embed_length = None
def _waveReader(self, path):
"""
read wave file and its corresponding
:param path: wav fi
:return:
"""
fs, wavsignal = wav.read(path)
self.fs = fs
if len(wavsignal) == 2:
self.channels = 2
self.left_signal = wavsignal[0]
self.right_signal = wavsignal[1]
else:
self.channels = 1
self.wavsignal = wavsignal
def _LSBReplace(self, wavsignal, secret_message):
"""
embed watermarking a single wave
:param secret_message: secret message
:return:
"""
# choose random embedding location with roulette
np.random.seed(self.seed)
roulette = np.random.rand(len(wavsignal))
stego = np.array(wavsignal)
k = 0
for i in range(len(wavsignal)):
if roulette[i] <= self.rate:
value = wavsignal[i]
if value < 0:
value = -value
negative = True
else:
negative = False
# embed secret bit
if k < len(secret_message) and int(secret_message[k]) == 0:
value = value & 0b1111111111111110
k += 1
elif k < len(secret_message) and int(secret_message[k]) == 1:
value = value | 0b0000000000000001
k += 1
if negative:
stego[i] = -value
else:
stego[i] = value
stego = np.array(stego).astype(np.int16)
return stego
def _saveWave(self, stego, cover_path, stego_path, inplace=False):
"""
save stego wave
:param stego: stego wavsignal
:param cover_path: cover path
:param stego_path: stego path
:param inplace: whether to save in cover path
:return:
"""
if inplace:
wav.write(cover_path, self.fs, stego)
else:
assert stego_path is not None
wav.write(stego_path, self.fs, stego)
def embed(self, cover_path, stego_path, secret_message, inplace=False):
"""
steganogaphy
:param cover_path: cover wave path
:param stego_path: stego wave path
:param secret_message: secret message
:param inplace: steganogarphy in place
:return:
"""
# add stop mark
secret_message = np.concatenate([secret_message, self.stop_mark], axis=0)
# pre check
self._waveReader(cover_path)
assert self.channels * len(self.wavsignal) * self.rate >= 1.1 * len(secret_message)
assert self.channels in [1, 2]
# embed secret message
if self.channels == 1:
if self.mode == 'single':
stego = self._LSBReplace(self.wavsignal, secret_message)
self._saveWave(stego, cover_path, stego_path, inplace)
elif self.mode == 'batch':
for i in range(len(cover_path)):
stego = self._LSBReplace(self.wavsignal, secret_message)
self._saveWave(stego, cover_path, stego_path, inplace)
elif self.channels == 2:
if self.mode == 'single':
left_stego = self._LSBReplace(self.left_signal, secret_message)
right_stego = self._LSBReplace(self.right_signal, secret_message)
stego = [left_stego, right_stego]
self._saveWave(stego, cover_path, stego_path, inplace)
elif self.mode == 'batch':
# the same secret messages are embedding in different carrier
for i in range(len(stego_path)):
left_stego = self._LSBReplace(self.left_signal, secret_message)
right_stego = self._LSBReplace(self.right_signal, secret_message)
stego = [left_stego, right_stego]
self._saveWave(stego, cover_path[i], stego_path[i], inplace)
class LSBExtractor:
def __init__(self, seed, rate=0.9):
self.seed = seed
self.stop_mark = stop_mark
self.rate = rate
self.fs = None
self.channels= None
self.wavsignal = None
self.left_signal = None
self.right_signal = None
def _waveReader(self, path):
"""
read wave file and its corresponding
:param path: wav fi
:return:
"""
fs, wavsignal = wav.read(path)
self.fs = fs
if len(wavsignal) == 2:
self.channels = 2
self.left_signal = wavsignal[0]
self.right_signal = wavsignal[1]
else:
self.channels = 1
self.wavsignal = wavsignal
def _checkHeader(self, header):
"""
check the validness of header
:param header: header
:return: True/False
"""
return True
def _checkStop(self, message):
"""
check stop
:param message: secret message
:return: True/False
"""
message_stop = message[-len(self.stop_mark):]
count = 0
for i in range(len(self.stop_mark)):
if message_stop[i] == self.stop_mark[i]:
count += 1
if count == len(self.stop_mark):
return True
else:
return False
def _LSBExtract(self, roulette, wavsignal):
"""
extract LSB from stego wavsignal
:param roulette:
:param wavsignal:
:return: secret message
"""
message = []
for i in range(len(wavsignal)):
if roulette[i] <= self.rate:
value = wavsignal[i]
value = '{:016b}'.format(value)
message.append(int(value[-1]))
# check the validness of header
if len(message) == 44:
assert self._checkHeader(message) is True
# check stop mark
if len(message) >= len(self.stop_mark) and self._checkStop(message):
return message
return message
def extract(self, wave_path, message_path):
"""
extract message in wave
:param wave_path: wave path
:param message_path: message path
:return:
"""
# choose random embedding location with roulette
self._waveReader(wave_path)
np.random.seed(self.seed)
roulette = np.random.rand(len(self.wavsignal))
if self.channels == 1:
message = self._LSBExtract(roulette, self.wavsignal)
elif self.channels == 2:
message_left = self._LSBExtract(roulette, self.left_signal)
message_right = self._LSBExtract(roulette, self.right_signal)
message = np.hstack((message_left, message_right))
with open(message_path, "w", encoding='utf-8') as f:
f.write(''.join(str(i) for i in message))
return message
def main():
np.random.seed(0)
wave_path = './org.wav'
stego_path = './marked.wav'
message_path = './s.txt'
secret_message = np.random.randint(0, 2, 1600)
alice = LSBEmbedder(0)
alice.embed(wave_path, stego_path, secret_message)
bob = LSBExtractor(0)
m = bob.extract(stego_path, message_path)
assert len(m) - len(stop_mark) == len(secret_message)
count = 0
for i in range(len(secret_message)):
if int(m[i]) != int(secret_message[i]):
count += 1
print('BER', count / len(m))
if __name__ == '__main__':
main()