Commit 45a82355190e35bda63d33c396ec0223d4215363

Authored by Chunk
1 parent eb820443
Exists in master

staged.

@@ -64,7 +64,8 @@ def bits2bytes(arry_bits): @@ -64,7 +64,8 @@ def bits2bytes(arry_bits):
64 """ 64 """
65 str_bits = ''.join(map(str, arry_bits)) 65 str_bits = ''.join(map(str, arry_bits))
66 arry_bytes = [int(str_bits[i:i + 8], 2) for i in range(0, len(str_bits), 8)] 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 if __name__ == '__main__': 71 if __name__ == '__main__':
common.pyc
No preview for this file type
msteg/StegBase.py
@@ -55,7 +55,7 @@ class StegBase(object): @@ -55,7 +55,7 @@ class StegBase(object):
55 for i in xrange(4): 55 for i in xrange(4):
56 raw[i] = raw_size % 256 56 raw[i] = raw_size % 256
57 raw_size /= 256 57 raw_size /= 256
58 - self.hid_data = np.array(raw) 58 + self.hid_data = np.array(raw, dtype=np.uint8)
59 59
60 if np.size(self.hid_data) * 8 > np.size(self.cov_data): 60 if np.size(self.hid_data) * 8 > np.size(self.cov_data):
61 raise Exception("Cover image is too small to embed data.Cannot fit %d bits in %d DCT coefficients" % ( 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,22 +71,28 @@ class StegBase(object):
71 try: 71 try:
72 cov_data = self._get_cov_data(src_cover) 72 cov_data = self._get_cov_data(src_cover)
73 hid_data = self._get_hid_data(src_hidden) 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 self.cov_jpeg.setCoefBlocks(cov_data) 81 self.cov_jpeg.setCoefBlocks(cov_data)
77 self.cov_jpeg.Jwrite(tgt_stego) 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 size_embedded = np.size(hid_data) 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 time.time() - self.t0) 89 time.time() - self.t0)
84 90
85 except TypeError as e: 91 except TypeError as e:
86 raise e 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 def _post_extract_actions(self, src_steg, tgt_hidden): 97 def _post_extract_actions(self, src_steg, tgt_hidden):
92 """ 98 """
@@ -95,11 +101,13 @@ class StegBase(object): @@ -95,11 +101,13 @@ class StegBase(object):
95 """ 101 """
96 try: 102 try:
97 steg_data = self._get_cov_data(src_steg) 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 # recovering file size 108 # recovering file size
101 header_size = 4 * 8 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 size_data = bits2bytes(size_data) 111 size_data = bits2bytes(size_data)
104 size_hd = 0 112 size_hd = 0
105 for i in xrange(4): 113 for i in xrange(4):
@@ -110,15 +118,22 @@ class StegBase(object): @@ -110,15 +118,22 @@ class StegBase(object):
110 if raw_size > np.size(steg_data): 118 if raw_size > np.size(steg_data):
111 raise Exception("Supposed secret data too large for stego image.") 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 hid_data = bits2bytes(hid_data) 127 hid_data = bits2bytes(hid_data)
  128 + # print hid_data.dtype,type(hid_data),hid_data.tolist()
115 hid_data[4:].tofile(tgt_hidden) 129 hid_data[4:].tofile(tgt_hidden)
116 130
117 self._display_stats("extracted", emb_size, 131 self._display_stats("extracted", emb_size,
118 np.size(hid_data), 132 np.size(hid_data),
119 time.time() - self.t0) 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 def _looks_like_jpeg(self, path): 139 def _looks_like_jpeg(self, path):
msteg/StegBase.pyc 0 โ†’ 100644
No preview for this file type
msteg/steganalysis/ChiSquare.py
@@ -16,11 +16,12 @@ Better Steganalysis @@ -16,11 +16,12 @@ Better Steganalysis
16 from collections import defaultdict 16 from collections import defaultdict
17 import os 17 import os
18 18
19 -import Image 19 +from PIL import Image
20 import numpy 20 import numpy
21 from scipy.stats import chisquare 21 from scipy.stats import chisquare
22 import matplotlib.pyplot as plt 22 import matplotlib.pyplot as plt
23 import itertools as it 23 import itertools as it
  24 +from msteg.StegBase import StegBase
24 25
25 from stegotool.util.plugins import describe_and_annotate 26 from stegotool.util.plugins import describe_and_annotate
26 from stegotool.util.plugins import ImagePath, NewFilePath 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,7 +12,7 @@ is embedded repeatedly in order to make non-ambiguous extraction possible.
12 import time 12 import time
13 import math 13 import math
14 import numpy as np 14 import numpy as np
15 -from StegBase import * 15 +from msteg.StegBase import StegBase
16 from common import * 16 from common import *
17 17
18 18
@@ -74,7 +74,7 @@ class F3(StegBase): @@ -74,7 +74,7 @@ class F3(StegBase):
74 i += 1 74 i += 1
75 if i == hid_data.size: break 75 if i == hid_data.size: break
76 76
77 - return cov_data 77 + return cov_data,i
78 78
79 def _raw_extract(self, steg_data, num_bits): 79 def _raw_extract(self, steg_data, num_bits):
80 """ 80 """
@@ -90,7 +90,7 @@ class F3(StegBase): @@ -90,7 +90,7 @@ class F3(StegBase):
90 hid_data[j] = x & 1 90 hid_data[j] = x & 1
91 j = j + 1 91 j = j + 1
92 92
93 - return hid_data 93 + return hid_data,j
94 94
95 def __str__(self): 95 def __str__(self):
96 return 'F3' 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,11 +9,11 @@ which is not included in the original description of F4.
9 """ 9 """
10 import time 10 import time
11 import numpy as np 11 import numpy as np
12 -from StegBase import * 12 +from msteg.StegBase import StegBase
13 from common import * 13 from common import *
14 14
15 15
16 -class F3(StegBase): 16 +class F4(StegBase):
17 """ This module has two methods: <i>embed_raw_data</i> to embed data 17 """ This module has two methods: <i>embed_raw_data</i> to embed data
18 with the F3 algorithm and <i>extract_raw_data</i> to extract data 18 with the F3 algorithm and <i>extract_raw_data</i> to extract data
19 which was embedded previously. """ 19 which was embedded previously. """
msteg/steganography/F4.pyc 0 โ†’ 100644
No preview for this file type
msteg/steganography/F5.pyc 0 โ†’ 100644
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,7 +10,7 @@ bits a parameter named word_size. Thus --- in this context --- word means
10 import time 10 import time
11 import numpy as np 11 import numpy as np
12 import scipy as sp 12 import scipy as sp
13 -from StegBase import * 13 +from msteg.StegBase import StegBase
14 from common import * 14 from common import *
15 15
16 16
@@ -66,7 +66,7 @@ class LSB(StegBase): @@ -66,7 +66,7 @@ class LSB(StegBase):
66 if x == 0 or x == 1 or cnt % 64 == 0: continue 66 if x == 0 or x == 1 or cnt % 64 == 0: continue
67 67
68 m = (hid_data[i] & 1) 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 i += 1 70 i += 1
71 if i == hid_data.size: break 71 if i == hid_data.size: break
72 72
msteg/steganography/LSB.pyc 0 โ†’ 100644
No preview for this file type
msteg/steganography/StegBase.pyc
No preview for this file type
@@ -186,7 +186,7 @@ if __name__ == &#39;__main__&#39;: @@ -186,7 +186,7 @@ if __name__ == &#39;__main__&#39;:
186 186
187 ima = jpegObj.Jpeg("res/test3.jpg") 187 ima = jpegObj.Jpeg("res/test3.jpg")
188 imb = jpegObj.Jpeg("res/steged.jpg") 188 imb = jpegObj.Jpeg("res/steged.jpg")
189 - 189 + print ima.Jgetcompdim(0)
190 diffblocks(ima, imb) 190 diffblocks(ima, imb)
191 191
192 c1 = ima.getCoefBlocks() 192 c1 = ima.getCoefBlocks()
@@ -7,7 +7,7 @@ import pylab as plt @@ -7,7 +7,7 @@ import pylab as plt
7 import mjpeg 7 import mjpeg
8 import mjsteg 8 import mjsteg
9 import jpegObj 9 import jpegObj
10 -from msteg.steganography import F3 10 +from msteg.steganography import F3, F4, LSB
11 from common import * 11 from common import *
12 12
13 13