8cfc1a23
Chunk
F5 half-finished.
|
1
2
|
__author__ = 'chunk'
|
c9fdeb00
Chunk
LSB and F4 added.
|
3
|
"""
|
26e2fe9f
Chunk
MPB steganalysis ...
|
4
5
|
ref - http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.115.3651&rep=rep1&type=pdf
|
c9fdeb00
Chunk
LSB and F4 added.
|
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<p>This module implements the rather sophisticated F5 algorithm which was
invented by Andreas Westfeld.</p>
Unlike its vastly inferior predecessors, namely F3 and F4, it features matrix
encoding which makes it possible to embed a chunk of k bits within 2^k - 1
bits of the cover data and only change one bit (at most). A bit change is
done by subtracting the absolute value of the corresponding DCT coefficient.
When the embedding process begins, the parameter k is computed based on
the capacity of the cover image and the prospective embedding ratio.
With small amount of hidden data k becomes large which leads to a greater
embedding efficiency (embedded information per bit change).<br />
A permutation (initialized by a user-supplied seed) of the DCT coefficients
helps to scatter each chunk across the entire image.
F5 can be seen as meta-algorithm as it uses a coding scheme to change
as little data as possible and then applies a simpler algorithm (such as F3)
to actually embed data. That is why this module allows the user to specify
which embedding function (one of JSteg, F3, F4) should be used.
"""
|
c9fdeb00
Chunk
LSB and F4 added.
|
26
27
|
import math
import numpy as np
|
033d3b0d
Chunk
staged.
|
28
|
import numpy.random as rnd
|
b69b6985
Chunk
py module refract...
|
29
|
from msteg import *
|
033d3b0d
Chunk
staged.
|
30
|
from F4 import F4
|
c6c61f81
Chunk
staged.
|
31
|
import mjpeg
|
033d3b0d
Chunk
staged.
|
32
|
from common import *
|
c9fdeb00
Chunk
LSB and F4 added.
|
33
34
|
|
033d3b0d
Chunk
staged.
|
35
|
class F5(StegBase):
|
c9fdeb00
Chunk
LSB and F4 added.
|
36
37
38
39
|
""" This module has two methods: <i>embed_raw_data</i> to embed data
with the F5 algorithm and <i>extract_raw_data</i> to extract data
which was embedded previously. """
|
8cfc1a23
Chunk
F5 half-finished.
|
40
|
def __init__(self, key=sample_key, k=None):
|
c9fdeb00
Chunk
LSB and F4 added.
|
41
42
43
|
"""
Constructor of the F5 class.
"""
|
033d3b0d
Chunk
staged.
|
44
|
StegBase.__init__(self, key)
|
c9fdeb00
Chunk
LSB and F4 added.
|
45
|
self._embed_fun = None
|
c9fdeb00
Chunk
LSB and F4 added.
|
46
|
self.default_embedding = True
|
8cfc1a23
Chunk
F5 half-finished.
|
47
|
|
c9fdeb00
Chunk
LSB and F4 added.
|
48
|
# needed because k is embedded separately
|
8cfc1a23
Chunk
F5 half-finished.
|
49
50
51
52
53
54
55
|
self.k_coeff = k
def _get_cov_data(self, img_path):
"""
Returns DCT coefficients of the cover image.
"""
|
c6c61f81
Chunk
staged.
|
56
|
self.cov_jpeg = mjpeg.Jpeg(img_path, key=self.key)
|
8cfc1a23
Chunk
F5 half-finished.
|
57
58
59
60
61
62
63
64
65
66
|
cov_data = self.cov_jpeg.getsignal(channel='Y')
self.cov_data = np.array(cov_data, dtype=np.int16)
return self.cov_data
def embed_raw_data(self, src_cover, src_hidden, tgt_stego, embed_fun='Default'):
"""This method embeds arbitrary data into a cover image.
The cover image must be a JPEG.
@param embed_fun:
|
c9fdeb00
Chunk
LSB and F4 added.
|
67
68
69
70
|
Specifies which embedding function should be used. Must be one of
'Default', 'F3', 'Jsteg'. If 'Default' is selected, the algorithm uses
the same behavior as Westfeld's implementation, i.e. decrementing
absolute values for n > 1 (F3) and using F4 in the special case n = 1.
|
8cfc1a23
Chunk
F5 half-finished.
|
71
|
Selecting F3 or JSteg results in using that scheme for all n.
|
c9fdeb00
Chunk
LSB and F4 added.
|
72
73
|
"""
self.t0 = time.time()
|
8cfc1a23
Chunk
F5 half-finished.
|
74
|
|
c9fdeb00
Chunk
LSB and F4 added.
|
75
76
77
|
if embed_fun == 'F3':
self._embed_fun = self._f3_embed
self.default_embedding = False
|
8cfc1a23
Chunk
F5 half-finished.
|
78
|
elif embed_fun == 'JSteg' or embed_fun == 'LSB':
|
c9fdeb00
Chunk
LSB and F4 added.
|
79
80
|
self._embed_fun = self._jsteg_embed
self.default_embedding = False
|
8cfc1a23
Chunk
F5 half-finished.
|
81
|
else:
|
c9fdeb00
Chunk
LSB and F4 added.
|
82
83
84
|
self._embed_fun = self._f3_embed
self.default_embedding = True
|
8cfc1a23
Chunk
F5 half-finished.
|
85
86
87
88
|
try:
cov_data = self._get_cov_data(src_cover)
hid_data = self._get_hid_data(src_hidden)
# print hid_data.dtype,type(hid_data),hid_data.tolist()
|
c9fdeb00
Chunk
LSB and F4 added.
|
89
|
|
8cfc1a23
Chunk
F5 half-finished.
|
90
|
cov_data, bits_cnt = self._raw_embed(cov_data, hid_data)
|
c9fdeb00
Chunk
LSB and F4 added.
|
91
|
|
8cfc1a23
Chunk
F5 half-finished.
|
92
93
94
|
if bits_cnt < np.size(hid_data) * 8:
raise Exception("Expected embedded size is %db but actually %db." % (
np.size(hid_data) * 8, bits_cnt))
|
c9fdeb00
Chunk
LSB and F4 added.
|
95
|
|
8cfc1a23
Chunk
F5 half-finished.
|
96
97
|
self.cov_jpeg.setsignal(cov_data, channel='Y')
self.cov_jpeg.Jwrite(tgt_stego)
|
c9fdeb00
Chunk
LSB and F4 added.
|
98
|
|
873557f9
Chunk
staged.
|
99
100
|
# size_cov = os.path.getsize(tgt_stego)
size_cov = np.size(cov_data) / 8
|
c9fdeb00
Chunk
LSB and F4 added.
|
101
|
size_embedded = np.size(hid_data)
|
873557f9
Chunk
staged.
|
102
103
104
105
106
107
108
|
self._display_stats("embedded", size_cov, size_embedded,
time.time() - self.t0)
except TypeError as e:
raise e
except Exception as expt:
|
c9fdeb00
Chunk
LSB and F4 added.
|
109
|
print "Exception when embedding!"
|
8cfc1a23
Chunk
F5 half-finished.
|
110
111
112
113
114
115
116
117
|
raise
def extract_raw_data(self, src_steg, tgt_hidden, embed_fun='Default'):
self.t0 = time.time()
if embed_fun == 'F3':
self._embed_fun = self._f3_embed
|
c9fdeb00
Chunk
LSB and F4 added.
|
118
|
self.default_embedding = False
|
8cfc1a23
Chunk
F5 half-finished.
|
119
120
121
122
123
124
|
elif embed_fun == 'JSteg' or embed_fun == 'LSB':
self._embed_fun = self._jsteg_embed
self.default_embedding = False
else:
self._embed_fun = self._f3_embed
self.default_embedding = True
|
c9fdeb00
Chunk
LSB and F4 added.
|
125
|
|
8cfc1a23
Chunk
F5 half-finished.
|
126
127
|
try:
steg_data = self._get_cov_data(src_steg)
|
c9fdeb00
Chunk
LSB and F4 added.
|
128
129
|
# emb_size = os.path.getsize(src_steg)
emb_size = np.size(steg_data) / 8
|
8cfc1a23
Chunk
F5 half-finished.
|
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
|
# recovering file size
header_size = 4 * 8
size_data, bits_cnt = self._raw_extract(steg_data, header_size)
if bits_cnt < header_size:
raise Exception("Expected embedded size is %db but actually %db." % (
header_size, bits_cnt))
size_data = bits2bytes(size_data[:header_size])
print size_data
size_hd = 0
for i in xrange(4):
size_hd += size_data[i] * 256 ** i
raw_size = size_hd * 8
if raw_size > np.size(steg_data):
raise Exception("Supposed secret data too large for stego image.")
hid_data, bits_cnt = self._raw_extract(steg_data, raw_size)
if bits_cnt < raw_size:
raise Exception("Expected embedded size is %db but actually %db." % (
raw_size, bits_cnt))
hid_data = bits2bytes(hid_data)
# print hid_data.dtype,type(hid_data),hid_data.tolist()
hid_data[4:].tofile(tgt_hidden)
self._display_stats("extracted", emb_size,
np.size(hid_data),
time.time() - self.t0)
except Exception as expt:
print "Exception when extracting!"
raise
def _embed_k(self, cov_data, hid_data):
np.random.seed(self.seed)
|
c9fdeb00
Chunk
LSB and F4 added.
|
171
|
self.dct_p = np.random.permutation(cov_data.size)
|
c9fdeb00
Chunk
LSB and F4 added.
|
172
173
174
175
176
177
178
179
180
181
182
183
184
|
self.k_coeff = self._find_max_k(cov_data, hid_data)
self.ui.display_status('setting k = %d' % self.k_coeff)
k_split = self.lookup_tab.split_byte(self.k_coeff, 1)[-4:]
# embed k in F3-like style
for m in k_split:
success = False
while not success:
self.cov_ind += 1
while cov_data[self.dct_p[self.cov_ind]] == 0 or \
self.dct_p[self.cov_ind] % 64 == 0:
self.cov_ind += 1
if m != cov_data[self.dct_p[self.cov_ind]] & 1:
cov_data[self.dct_p[self.cov_ind]] -= \
|
033d3b0d
Chunk
staged.
|
185
|
math.copysign(1, cov_data[self.dct_p[self.cov_ind]])
|
c9fdeb00
Chunk
LSB and F4 added.
|
186
187
188
189
190
191
192
193
194
195
196
197
198
|
success = cov_data[self.dct_p[self.cov_ind]] != 0
def _extract_k(self, steg_data):
# initializing the MT is done only once in order to retain the state
self.dct_p = np.random.seed(self.seed)
self.dct_p = np.random.permutation(self.steg_data.size)
k_split = np.zeros(4, np.uint8)
for i in xrange(k_split.size):
self.steg_ind += 1
while self.steg_data[self.dct_p[self.steg_ind]] == 0 or \
self.dct_p[self.steg_ind] % 64 == 0:
self.steg_ind += 1
k_split[i] = self.steg_data[self.dct_p[self.steg_ind]] & 1
|
033d3b0d
Chunk
staged.
|
199
200
|
self.k_coeff = self.lookup_tab.merge_words(tuple([0, 0, 0, 0] +
list(k_split)), 1)
|
c9fdeb00
Chunk
LSB and F4 added.
|
201
202
203
204
205
206
207
208
209
210
211
212
|
def _find_max_k(self, cov_data, hid_data):
cnt = 4 # information about k take up 4 bits
# find number of DCT coefficients
update_cnt = 10000
for i, c in enumerate(cov_data):
if update_cnt == 0:
self._set_progress(
int(30 * (float(i) / float(cov_data.size))))
update_cnt = 10000
update_cnt -= 1
# pessimistic, but accurate estimation of the capacity of the image
|
033d3b0d
Chunk
staged.
|
213
|
ci = int(c)
|
c9fdeb00
Chunk
LSB and F4 added.
|
214
215
216
217
218
219
220
221
222
223
224
225
226
|
if (not (ci is 0)) and (not ((i % 64) is 0)) \
and (not (ci is 1)) and (not (ci is -1)):
cnt += 1
hid_size = hid_data.size
cov_size = cnt
if cov_size < hid_size:
raise Exception("Cannot fit %d bits in %d DCT coefficients. \
Cover image is too small." % (hid_size, cov_size))
self.ui.display_status('DCT embedding ratio = %f' \
% (float(hid_size) / float(cov_size)))
k = 1
while True:
k += 1
|
033d3b0d
Chunk
staged.
|
227
|
n = (1 << k) - 1
|
c9fdeb00
Chunk
LSB and F4 added.
|
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
num_chunks = cov_size / n
num_emb_bits = num_chunks * k
if num_emb_bits < hid_size:
return min(k - 1, 15)
# low level embedding functions
def _f3_embed(self, cov_data, ind):
cov_data[ind] -= math.copysign(1, cov_data[ind])
def _jsteg_embed(self, cov_data, ind):
m = 1 ^ (cov_data[ind] & 1)
cov_data[ind] = (cov_data[ind] & 0xfffffffe) | m
def _raw_embed(self, cov_data, hid_data):
k = self.k_coeff
|
8cfc1a23
Chunk
F5 half-finished.
|
243
|
n = (1 << k) - 1
|
c9fdeb00
Chunk
LSB and F4 added.
|
244
|
|
8cfc1a23
Chunk
F5 half-finished.
|
245
|
if n == 1 and self.default_embedding:
|
c9fdeb00
Chunk
LSB and F4 added.
|
246
247
|
# in case k = n = 1, Westfeld's implementation uses F4 for embedding.
f4 = F4(key=self.key)
|
8cfc1a23
Chunk
F5 half-finished.
|
248
|
return f4._raw_embed(cov_data, hid_data)
|
c9fdeb00
Chunk
LSB and F4 added.
|
249
|
|
8cfc1a23
Chunk
F5 half-finished.
|
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
hid_data = bytes2bits(hid_data)
if len(hid_data) % k != 0:
hid_data = list(hid_data) + [0 for x in range(k - len(hid_data) % k)]
ind_nonzero = np.nonzero(cov_data)[0]
if np.size(ind_nonzero) * k < len(hid_data) * n:
raise Exception("Supposed secret data too large for stego image.")
ind_cov = 0
for ind_hid in range(0, len(hid_data), k):
msg_chunk = hid_data[ind_hid:ind_hid + k]
cov_chunk = ind_nonzero[ind_cov:ind_cov + n]
ind_cov += n
success = False
while not success:
h = 0
|
c9fdeb00
Chunk
LSB and F4 added.
|
268
269
|
for i in xrange(n):
h ^= ((cov_data[cov_chunk[i]] & 1) * (i + 1))
|
8cfc1a23
Chunk
F5 half-finished.
|
270
|
scalar_x = 0
|
c9fdeb00
Chunk
LSB and F4 added.
|
271
272
273
274
275
|
for i in xrange(k):
scalar_x = (scalar_x << 1) + msg_chunk[
i] # N.B. hid_data[0]:high (that is x2), hid_data[1]:low (that is x1)
s = scalar_x ^ h
if s != 0:
|
8cfc1a23
Chunk
F5 half-finished.
|
276
277
|
self._embed_fun(cov_data, cov_chunk[s - 1])
else:
|
c9fdeb00
Chunk
LSB and F4 added.
|
278
279
280
281
282
283
|
break
if cov_data[cov_chunk[s - 1]] == 0: # shrinkage
cov_chunk[s - 1:-1] = cov_chunk[s:]
cov_chunk[-1] = ind_nonzero[ind_cov]
ind_cov += 1
|
8cfc1a23
Chunk
F5 half-finished.
|
284
285
286
287
|
else:
success = True
return cov_data, ind_hid + k
|
c9fdeb00
Chunk
LSB and F4 added.
|
288
289
290
|
def _raw_extract(self, steg_data, num_bits):
k = self.k_coeff
|
8cfc1a23
Chunk
F5 half-finished.
|
291
|
n = (1 << k) - 1
|
c9fdeb00
Chunk
LSB and F4 added.
|
292
|
|
8cfc1a23
Chunk
F5 half-finished.
|
293
|
if n == 1 and self.default_embedding:
|
c9fdeb00
Chunk
LSB and F4 added.
|
294
295
|
f4 = F4(key=self.key)
return f4._raw_extract(steg_data, num_bits)
|
c9fdeb00
Chunk
LSB and F4 added.
|
296
|
|
8cfc1a23
Chunk
F5 half-finished.
|
297
298
299
|
num_bits_ceil = num_bits
if num_bits % k != 0:
num_bits_ceil = k * (num_bits / k + 1)
|
c9fdeb00
Chunk
LSB and F4 added.
|
300
|
|
8cfc1a23
Chunk
F5 half-finished.
|
301
302
303
|
hid_data = np.zeros(num_bits_ceil, np.uint8)
curr_chunk = np.zeros(k, np.uint8)
steg_data = steg_data[np.nonzero(steg_data)]
|
c9fdeb00
Chunk
LSB and F4 added.
|
304
|
ind_hid = 0
|
8cfc1a23
Chunk
F5 half-finished.
|
305
306
307
308
309
310
|
for ind_cov in range(0, len(steg_data), n):
steg_chunk = steg_data[ind_cov:ind_cov + n]
h = 0 # hash value
for i in xrange(n):
h ^= ((steg_chunk[i] & 1) * (i + 1))
|
c9fdeb00
Chunk
LSB and F4 added.
|
311
312
313
314
315
316
|
for i in xrange(k):
curr_chunk[k - i - 1] = h & 1 # N.B. hid_data[0]:high (that is x2), hid_data[1]:low (that is x1)
h >>= 1
hid_data[ind_hid:ind_hid + k] = curr_chunk[0:k]
|
8cfc1a23
Chunk
F5 half-finished.
|
317
318
|
ind_hid += k
|
c9fdeb00
Chunk
LSB and F4 added.
|
319
|
if ind_hid >= num_bits_ceil: break
|
8cfc1a23
Chunk
F5 half-finished.
|
320
321
|
return hid_data, num_bits_ceil
|
c9fdeb00
Chunk
LSB and F4 added.
|
322
|
|
8cfc1a23
Chunk
F5 half-finished.
|
323
|
def __str__(self):
|
c9fdeb00
Chunk
LSB and F4 added.
|
324
|
return 'F5'
|
8cfc1a23
Chunk
F5 half-finished.
|
|
|
c9fdeb00
Chunk
LSB and F4 added.
|
|
|