Commit 45a82355190e35bda63d33c396ec0223d4215363
1 parent
eb820443
Exists in
master
staged.
Showing
15 changed files
with
40 additions
and
23 deletions
Show diff stats
common.py
... | ... | @@ -64,7 +64,8 @@ def bits2bytes(arry_bits): |
64 | 64 | """ |
65 | 65 | str_bits = ''.join(map(str, arry_bits)) |
66 | 66 | arry_bytes = [int(str_bits[i:i + 8], 2) for i in range(0, len(str_bits), 8)] |
67 | - return np.array(arry_bytes).ravel() | |
67 | + return np.array(arry_bytes,dtype=np.uint8).ravel() | |
68 | + | |
68 | 69 | |
69 | 70 | |
70 | 71 | if __name__ == '__main__': | ... | ... |
common.pyc
No preview for this file type
msteg/StegBase.py
... | ... | @@ -55,7 +55,7 @@ class StegBase(object): |
55 | 55 | for i in xrange(4): |
56 | 56 | raw[i] = raw_size % 256 |
57 | 57 | raw_size /= 256 |
58 | - self.hid_data = np.array(raw) | |
58 | + self.hid_data = np.array(raw, dtype=np.uint8) | |
59 | 59 | |
60 | 60 | if np.size(self.hid_data) * 8 > np.size(self.cov_data): |
61 | 61 | raise Exception("Cover image is too small to embed data.Cannot fit %d bits in %d DCT coefficients" % ( |
... | ... | @@ -71,22 +71,28 @@ class StegBase(object): |
71 | 71 | try: |
72 | 72 | cov_data = self._get_cov_data(src_cover) |
73 | 73 | hid_data = self._get_hid_data(src_hidden) |
74 | - cov_data = self._raw_embed(cov_data, hid_data) | |
74 | + # print hid_data.dtype,type(hid_data),hid_data.tolist() | |
75 | + cov_data, bits_cnt = self._raw_embed(cov_data, hid_data) | |
76 | + | |
77 | + if bits_cnt != np.size(hid_data) * 8: | |
78 | + raise Exception("Expected embedded size is %db but actually %db." % ( | |
79 | + np.size(hid_data) * 8, bits_cnt)) | |
75 | 80 | |
76 | 81 | self.cov_jpeg.setCoefBlocks(cov_data) |
77 | 82 | self.cov_jpeg.Jwrite(tgt_stego) |
78 | 83 | |
79 | - size = os.path.getsize(tgt_stego) | |
84 | + # size_cov = os.path.getsize(tgt_stego) | |
85 | + size_cov = np.size(cov_data) / 8 | |
80 | 86 | size_embedded = np.size(hid_data) |
81 | 87 | |
82 | - self._display_stats("embedded", size, size_embedded, | |
88 | + self._display_stats("embedded", size_cov, size_embedded, | |
83 | 89 | time.time() - self.t0) |
84 | 90 | |
85 | 91 | except TypeError as e: |
86 | 92 | raise e |
87 | - except Exception: | |
88 | - raise Exception( | |
89 | - 'DCT coefficients exhausted. This usually means there are not enough DCT coefficients in the image in which algorithm can actually embed data. You should choose a larger image.') | |
93 | + except Exception as expt: | |
94 | + print "Exception when embedding!" | |
95 | + raise | |
90 | 96 | |
91 | 97 | def _post_extract_actions(self, src_steg, tgt_hidden): |
92 | 98 | """ |
... | ... | @@ -95,11 +101,13 @@ class StegBase(object): |
95 | 101 | """ |
96 | 102 | try: |
97 | 103 | steg_data = self._get_cov_data(src_steg) |
98 | - emb_size = os.path.getsize(src_steg) | |
104 | + # emb_size = os.path.getsize(src_steg) | |
105 | + emb_size = np.size(steg_data) / 8 | |
106 | + | |
99 | 107 | |
100 | 108 | # recovering file size |
101 | 109 | header_size = 4 * 8 |
102 | - size_data = self._raw_extract(steg_data, header_size) | |
110 | + size_data, bits_cnt = self._raw_extract(steg_data, header_size) | |
103 | 111 | size_data = bits2bytes(size_data) |
104 | 112 | size_hd = 0 |
105 | 113 | for i in xrange(4): |
... | ... | @@ -110,15 +118,22 @@ class StegBase(object): |
110 | 118 | if raw_size > np.size(steg_data): |
111 | 119 | raise Exception("Supposed secret data too large for stego image.") |
112 | 120 | |
113 | - hid_data = self._raw_extract(steg_data, raw_size) | |
121 | + hid_data, bits_cnt = self._raw_extract(steg_data, raw_size) | |
122 | + | |
123 | + if bits_cnt != raw_size: | |
124 | + raise Exception("Expected embedded size is %db but actually %db." % ( | |
125 | + raw_size, bits_cnt)) | |
126 | + | |
114 | 127 | hid_data = bits2bytes(hid_data) |
128 | + # print hid_data.dtype,type(hid_data),hid_data.tolist() | |
115 | 129 | hid_data[4:].tofile(tgt_hidden) |
116 | 130 | |
117 | 131 | self._display_stats("extracted", emb_size, |
118 | 132 | np.size(hid_data), |
119 | 133 | time.time() - self.t0) |
120 | - except: | |
121 | - raise Exception('DCT coefficients exhausted.The stego image is probably corrupted.') | |
134 | + except Exception as expt: | |
135 | + print "Exception when extracting!" | |
136 | + raise | |
122 | 137 | |
123 | 138 | |
124 | 139 | def _looks_like_jpeg(self, path): | ... | ... |
No preview for this file type
msteg/steganalysis/ChiSquare.py
... | ... | @@ -16,11 +16,12 @@ Better Steganalysis |
16 | 16 | from collections import defaultdict |
17 | 17 | import os |
18 | 18 | |
19 | -import Image | |
19 | +from PIL import Image | |
20 | 20 | import numpy |
21 | 21 | from scipy.stats import chisquare |
22 | 22 | import matplotlib.pyplot as plt |
23 | 23 | import itertools as it |
24 | +from msteg.StegBase import StegBase | |
24 | 25 | |
25 | 26 | from stegotool.util.plugins import describe_and_annotate |
26 | 27 | from stegotool.util.plugins import ImagePath, NewFilePath | ... | ... |
msteg/steganography/F3.py
... | ... | @@ -12,7 +12,7 @@ is embedded repeatedly in order to make non-ambiguous extraction possible. |
12 | 12 | import time |
13 | 13 | import math |
14 | 14 | import numpy as np |
15 | -from StegBase import * | |
15 | +from msteg.StegBase import StegBase | |
16 | 16 | from common import * |
17 | 17 | |
18 | 18 | |
... | ... | @@ -74,7 +74,7 @@ class F3(StegBase): |
74 | 74 | i += 1 |
75 | 75 | if i == hid_data.size: break |
76 | 76 | |
77 | - return cov_data | |
77 | + return cov_data,i | |
78 | 78 | |
79 | 79 | def _raw_extract(self, steg_data, num_bits): |
80 | 80 | """ |
... | ... | @@ -90,7 +90,7 @@ class F3(StegBase): |
90 | 90 | hid_data[j] = x & 1 |
91 | 91 | j = j + 1 |
92 | 92 | |
93 | - return hid_data | |
93 | + return hid_data,j | |
94 | 94 | |
95 | 95 | def __str__(self): |
96 | 96 | return 'F3' | ... | ... |
msteg/steganography/F3.pyc
No preview for this file type
msteg/steganography/F4.py
... | ... | @@ -9,11 +9,11 @@ which is not included in the original description of F4. |
9 | 9 | """ |
10 | 10 | import time |
11 | 11 | import numpy as np |
12 | -from StegBase import * | |
12 | +from msteg.StegBase import StegBase | |
13 | 13 | from common import * |
14 | 14 | |
15 | 15 | |
16 | -class F3(StegBase): | |
16 | +class F4(StegBase): | |
17 | 17 | """ This module has two methods: <i>embed_raw_data</i> to embed data |
18 | 18 | with the F3 algorithm and <i>extract_raw_data</i> to extract data |
19 | 19 | which was embedded previously. """ | ... | ... |
No preview for this file type
No preview for this file type
msteg/steganography/LSB.py
... | ... | @@ -10,7 +10,7 @@ bits a parameter named word_size. Thus --- in this context --- word means |
10 | 10 | import time |
11 | 11 | import numpy as np |
12 | 12 | import scipy as sp |
13 | -from StegBase import * | |
13 | +from msteg.StegBase import StegBase | |
14 | 14 | from common import * |
15 | 15 | |
16 | 16 | |
... | ... | @@ -66,7 +66,7 @@ class LSB(StegBase): |
66 | 66 | if x == 0 or x == 1 or cnt % 64 == 0: continue |
67 | 67 | |
68 | 68 | m = (hid_data[i] & 1) |
69 | - x[...] = (x & 0xfffffffe) | m | |
69 | + x[...] = (x & 0xfffffffe) | m # '0xfffe' is enough, for elements are expected to be short integers. | |
70 | 70 | i += 1 |
71 | 71 | if i == hid_data.size: break |
72 | 72 | ... | ... |
No preview for this file type
msteg/steganography/StegBase.pyc
No preview for this file type
test_jpeg.py