-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsteganography.py
295 lines (247 loc) · 8.42 KB
/
steganography.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
@author: Tanmoy Das Gupta, Sandeepan Sengupta, Tamojit Saha
"""
import os
import numpy as np
from matplotlib.pyplot import imread, imsave
setup_file = 'setup.cfg'
from configurator import configurator as cfgr
CHANNEL, PLANE, SCOPE, BUFFER = cfgr(setup_file)
MSG_FILE = 'msg.txt'
IMAGE_FILE = 'image.tiff'
#==============================================================================
# Channel Auto-Selection Module
#==============================================================================
def validate(msg_file=MSG_FILE, image_file=IMAGE_FILE, channel=CHANNEL):
from coder import hide
msg_bin = hide(msg_file)
msg_length = len(msg_bin)
from entropy import ENTROPY
from analyze import analyze
address_r, chunk_r = analyze(msg_length, 'image.tiff', 'R')
address_g, chunk_g = analyze(msg_length, 'image.tiff', 'G')
address_b, chunk_b = analyze(msg_length, 'image.tiff', 'B')
entropies = []
restrict = []
if np.size(chunk_r) > 0:
if chunk_r[-1]-chunk_r[0] >= msg_length:
entropies.append(ENTROPY(imread(image_file))[0])
else:
entropies.append(0)
restrict.append('R')
else:
entropies.append(0)
restrict.append('R')
if np.size(chunk_g) > 0:
if chunk_g[-1]-chunk_g[0] >= msg_length:
entropies.append(ENTROPY(imread(image_file))[1])
else:
entropies.append(0)
restrict.append('G')
else:
entropies.append(0)
restrict.append('G')
if np.size(chunk_b) > 0:
if chunk_b[-1]-chunk_b[0] >= msg_length:
entropies.append(ENTROPY(imread(image_file))[2])
else:
entropies.append(0)
restrict.append('B')
else:
entropies.append(0)
restrict.append('B')
CHANNEL = np.argmax(entropies)
if CHANNEL == 0:
CHANNEL = 'R'
elif CHANNEL == 1:
CHANNEL = 'G'
elif CHANNEL == 2:
CHANNEL = 'B'
if np.size(restrict) == 3:
return None
elif channel in restrict:
# print '\n\nNOTE =>> Channel switched to '+CHANNEL+'\n\n' #Notice<=
return CHANNEL
else:
return channel
#==============================================================================
def maker(image, channel, plane, bitplane):
from numpy import uint8, zeros_like, bitwise_and
bitplane_array = np.reshape(bitplane, (-1, 512))
bitplane_array = bitplane_array*pow(2, plane) #correction required <=
image_data = 0
if channel == 'R':
image_data = image[:, :, 0]
elif channel == 'G':
image_data = image[:, :, 1]
elif channel == 'B':
image_data = image[:, :, 2]
bitplane_all = zeros_like(image_data, dtype=uint8)
bitplane_all[:] = 255 - pow(2, plane) #correction required <=
image_bitplane_cleared = bitwise_and(image_data, bitplane_all)
if channel == 'R':
image_r = image_bitplane_cleared[:, :] + bitplane_array
image_g = image[:, :, 1]
image_b = image[:, :, 2]
elif channel == 'G':
image_r = image[:, :, 0]
image_g = image_bitplane_cleared[:, :] + bitplane_array
image_b = image[:, :, 2]
elif channel == 'B':
image_r = image[:, :, 0]
image_g = image[:, :, 1]
image_b = image_bitplane_cleared[:, :] + bitplane_array
output_image = zeros_like(image, dtype=uint8)
output_image[:, :, 0] = image_r
output_image[:, :, 1] = image_g
output_image[:, :, 2] = image_b
output_image[:, :, 3] = 255
return output_image
def embed(msg_file=MSG_FILE, password=[-1, 2.01, 3], image_file=IMAGE_FILE, channel=CHANNEL, plane=PLANE, scope=SCOPE, ID=BUFFER):
"""
Test usage:
embed('msg.txt', [-1, 2.01, 3], 'image.tiff')
embed('msg.txt', [-1, 2.01, 3], 'image.tiff', 'R', 0, 5, 64)
"""
#==============================================================================
# Filtering inputs
#==============================================================================
try:
open(image_file) and open(msg_file)
except IOError:
return None
except AttributeError:
return None
try:
float(password[0])
except TypeError:
return None
except ValueError:
return None
except AttributeError:
return None
if channel == 'r' or channel == 'R' or channel == '0' or channel == 0:
channel = 'R'
elif channel == 'g' or channel == 'G' or channel == '1' or channel == 1:
channel = 'G'
elif channel == 'b' or channel == 'B' or channel == '2' or channel == 2:
channel = 'B'
else:
channel = 'R' #Set channel to RED in case of incorrect input
try:
plane = abs(int(plane))
if plane > 7:
plane = 7
except ValueError:
return None
try:
ID = abs(int(ID))
if ID < 4:
ID = 4
if ID%4 != 0:
ID = 4*(1+ID/4)
except ValueError:
return None
try:
scope = abs(int(scope))
if scope%2 == 0:
scope += 1
if scope > 9:
scope = 9
except ValueError:
return None
try:
imread(image_file)
try:
imread(msg_file)
return None
except IOError:
image = imread(image_file)
except IOError:
try:
image = imread(msg_file)
image_file, msg_file = msg_file, image_file
except IOError:
return None
#==============================================================================
channel=validate(msg_file, image_file, channel)#Switch to optimum channel<=
if channel is None:
return None
elif channel != CHANNEL:
# print 'Replace ``Channel`` value in ``setup_file`` with '+channel
from configurator import reconfigure as rcfg
rcfg(setup_file, 'Channel', channel) #filename, Keyword, newValue
# CHANNEL = channel #Updating value for cross function use: Not Working
from numpy import shape
rows, columns, channels = shape(image)
from cryptography import encrypt
encrypted_bitplane = encrypt(image_file, msg_file, password, channel, plane, scope, ID)
if np.size(encrypted_bitplane) == rows*columns:
output_image = maker(image, channel, plane, encrypted_bitplane)
base = os.path.basename(image_file)
name = os.path.splitext(base)[0]
ext = os.path.splitext(base)[1]
new_image_file = name+"_encoded"+ext
imsave(new_image_file, output_image, format='tiff')
else:
return None
def decipher(password=[-1, 2.01, 3], image_file='image_encoded.tiff', channel=CHANNEL, plane=PLANE, ID=BUFFER):
"""
Test usage:
decipher([-1, 2.01, 3], 'image_encoded.tiff')
decipher([-1, 2.01, 3], 'image_encoded.tiff', 'R', 0, 64)
"""
#==============================================================================
# Filtering inputs
#==============================================================================
try:
imread(image_file)
except IOError:
return None
except AttributeError:
return None
try:
float(password[0])
except TypeError:
return None
except ValueError:
return None
except AttributeError:
return None
if channel == 'r' or channel == 'R' or channel == '0' or channel == 0:
channel = 'R'
elif channel == 'g' or channel == 'G' or channel == '1' or channel == 1:
channel = 'G'
elif channel == 'b' or channel == 'B' or channel == '2' or channel == 2:
channel = 'B'
else:
channel = 'R' #Set channel to RED in case of incorrect input
try:
plane = abs(int(plane))
if plane > 7:
plane = 7
except ValueError:
return None
try:
ID = abs(int(ID))
if ID < 4:
ID = 4
if ID%4 != 0:
ID = 4*(1+ID/4)
except ValueError:
return None
#==============================================================================
from cryptography import decrypt
msg = decrypt(image_file, password, channel, plane, ID)
if msg != None:
name = os.path.splitext(os.path.basename(image_file))[0]
with open(name+"_deciphered.txt", "wb") as FILE:
FILE.write(msg)
return msg
else:
return None
if __name__ == '__main__':
embed()
# decipher()