-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathencr.py
373 lines (334 loc) · 10.2 KB
/
encr.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
from PIL import Image
import tkinter as tk
from tkinter import filedialog
import hashlib
import binascii
import textwrap
import cv2
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import sys
from importlib import reload
from bisect import bisect_left as bsearch
'''
GLOBAL Constants
'''
# Lorenz paramters and initial conditions
a, b, c = 10, 2.667, 28
x0, y0, z0 = 0, 0, 0
#DNA-Encoding RULE #1 A = 00, T=01, G=10, C=11
dna={}
dna["00"]="A"
dna["01"]="T"
dna["10"]="G"
dna["11"]="C"
dna["A"]=[0,0]
dna["T"]=[0,1]
dna["G"]=[1,0]
dna["C"]=[1,1]
#DNA xor
dna["AA"]=dna["TT"]=dna["GG"]=dna["CC"]="A"
dna["AG"]=dna["GA"]=dna["TC"]=dna["CT"]="G"
dna["AC"]=dna["CA"]=dna["GT"]=dna["TG"]="C"
dna["AT"]=dna["TA"]=dna["CG"]=dna["GC"]="T"
# Maximum time point and total number of time points
tmax, N = 100, 10000
def lorenz(X, t, a, b, c):
x, y, z = X
x_dot = -a*(x - y)
y_dot = c*x - y - x*z
z_dot = -b*z + x*y
return x_dot, y_dot, z_dot
def image_selector(): #returns path to selected image
path = "NULL"
root = tk.Tk()
root.withdraw() # we don't want a full GUI, so keep the root window from appearing
path = filedialog.askopenfilename() # show an "Open" dialog box and return the path to the selected file
if path!="NULL":
print("Image loaded!")
else:
print("Error Image not loaded!")
return path
def split_into_rgb_channels(image):
red = image[:,:,2]
green = image[:,:,1]
blue = image[:,:,0]
return red, green, blue
#Secure key generation
def securekey (iname):
img = Image.open(iname)
m, n = img.size
print("pixels: {0} width: {2} height: {1} ".format(m*n, m, n))
pix = img.load()
plainimage = list() #_plainimage contains all the rgb values continuously
for y in range(n):
for x in range(m):
for k in range(0,3):
plainimage.append(pix[x,y][k])
key = hashlib.sha256() #key is made a hash.sha256 object
key.update(bytearray(plainimage)) #image data is fed to generate digest
return key.hexdigest() ,m ,n
def update_lorentz (key):
key_bin = bin(int(key, 16))[2:].zfill(256) #covert hex key digest to binary
k={} #key dictionary
key_32_parts=textwrap.wrap(key_bin, 8) #slicing key into 8 parts
num=1
for i in key_32_parts:
k["k{0}".format(num)]=i
num = num + 1
t1 = t2 = t3 = 0
for i in range (1,12):
t1=t1^int(k["k{0}".format(i)],2)
for i in range (12,23):
t2=t2^int(k["k{0}".format(i)],2)
for i in range (23,33):
t3=t3^int(k["k{0}".format(i)],2)
global x0 ,y0, z0
x0=x0 + t1/256
y0=y0 + t2/256
z0=z0 + t3/256
def decompose_matrix(iname):
image = cv2.imread(iname)
blue,green,red = split_into_rgb_channels(image)
for values, channel in zip((red, green, blue), (2,1,0)):
img = np.zeros((values.shape[0], values.shape[1]), dtype = np.uint8)
img[:,:] = (values)
if channel == 0:
B = np.asmatrix(img)
elif channel == 1:
G = np.asmatrix(img)
else:
R = np.asmatrix(img)
return B,G,R
def dna_encode(b,g,r):
b = np.unpackbits(b,axis=1)
g = np.unpackbits(g,axis=1)
r = np.unpackbits(r,axis=1)
m,n = b.shape
r_enc= np.chararray((m,int(n/2)))
g_enc= np.chararray((m,int(n/2)))
b_enc= np.chararray((m,int(n/2)))
for color,enc in zip((b,g,r),(b_enc,g_enc,r_enc)):
idx=0
for j in range(0,m):
for i in range(0,n,2):
enc[j,idx]=dna["{0}{1}".format(color[j,i],color[j,i+1])]
idx+=1
if (i==n-2):
idx=0
break
b_enc=b_enc.astype(str)
g_enc=g_enc.astype(str)
r_enc=r_enc.astype(str)
return b_enc,g_enc,r_enc
def key_matrix_encode(key,b):
#encoded key matrix
b = np.unpackbits(b,axis=1)
m,n = b.shape
key_bin = bin(int(key, 16))[2:].zfill(256)
Mk = np.zeros((m,n),dtype=np.uint8)
x=0
for j in range(0,m):
for i in range(0,n):
Mk[j,i]=key_bin[x%256]
x+=1
Mk_enc=np.chararray((m,int(n/2)))
idx=0
for j in range(0,m):
for i in range(0,n,2):
if idx==(n/2):
idx=0
Mk_enc[j,idx]=dna["{0}{1}".format(Mk[j,i],Mk[j,i+1])]
idx+=1
Mk_enc=Mk_enc.astype(str)
return Mk_enc
def xor_operation(b,g,r,mk):
m,n = b.shape
bx=np.chararray((m,n))
gx=np.chararray((m,n))
rx=np.chararray((m,n))
b=b.astype(str)
g=g.astype(str)
r=r.astype(str)
for i in range(0,m):
for j in range (0,n):
bx[i,j] = dna["{0}{1}".format(b[i,j],mk[i,j])]
gx[i,j] = dna["{0}{1}".format(g[i,j],mk[i,j])]
rx[i,j] = dna["{0}{1}".format(r[i,j],mk[i,j])]
bx=bx.astype(str)
gx=gx.astype(str)
rx=rx.astype(str)
return bx,gx,rx
def gen_chaos_seq(m,n):
global x0,y0,z0,a,b,c,N
N=m*n*4
x= np.array((m,n*4))
y= np.array((m,n*4))
z= np.array((m,n*4))
t = np.linspace(0, tmax, N)
f = odeint(lorenz, (x0, y0, z0), t, args=(a, b, c))
x, y, z = f.T
x=x[:(N)]
y=y[:(N)]
z=z[:(N)]
return x,y,z
def plot(x,y,z):
fig = plt.figure()
ax = fig.gca(projection='3d')
s = 100
c = np.linspace(0,1,N)
for i in range(0,N-s,s):
ax.plot(x[i:i+s+1], y[i:i+s+1], z[i:i+s+1], color=(1-c[i],c[i],1), alpha=0.4)
ax.set_axis_off()
plt.show()
def sequence_indexing(x,y,z):
n=len(x)
fx=np.zeros((n),dtype=np.uint32)
fy=np.zeros((n),dtype=np.uint32)
fz=np.zeros((n),dtype=np.uint32)
seq=sorted(x)
for k1 in range(0,n):
t = x[k1]
k2 = bsearch(seq, t)
fx[k1]=k2
seq=sorted(y)
for k1 in range(0,n):
t = y[k1]
k2 = bsearch(seq, t)
fy[k1]=k2
seq=sorted(z)
for k1 in range(0,n):
t = z[k1]
k2 = bsearch(seq, t)
fz[k1]=k2
return fx,fy,fz
def scramble(fx,fy,fz,b,r,g):
p,q=b.shape
size = p*q
bx=b.reshape(size).astype(str)
gx=g.reshape(size).astype(str)
rx=r.reshape(size).astype(str)
bx_s=np.chararray((size))
gx_s=np.chararray((size))
rx_s=np.chararray((size))
for i in range(size):
idx = fz[i]
bx_s[i] = bx[idx]
for i in range(size):
idx = fy[i]
gx_s[i] = gx[idx]
for i in range(size):
idx = fx[i]
rx_s[i] = rx[idx]
bx_s=bx_s.astype(str)
gx_s=gx_s.astype(str)
rx_s=rx_s.astype(str)
b_s=np.chararray((p,q))
g_s=np.chararray((p,q))
r_s=np.chararray((p,q))
b_s=bx_s.reshape(p,q)
g_s=gx_s.reshape(p,q)
r_s=rx_s.reshape(p,q)
return b_s,g_s,r_s
def scramble_new(fx,fy,fz,b,g,r):
p,q=b.shape
size = p*q
bx=b.reshape(size)
gx=g.reshape(size)
rx=r.reshape(size)
bx_s=b.reshape(size)
gx_s=g.reshape(size)
rx_s=r.reshape(size)
bx=bx.astype(str)
gx=gx.astype(str)
rx=rx.astype(str)
bx_s=bx_s.astype(str)
gx_s=gx_s.astype(str)
rx_s=rx_s.astype(str)
for i in range(size):
idx = fz[i]
bx_s[idx] = bx[i]
for i in range(size):
idx = fy[i]
gx_s[idx] = gx[i]
for i in range(size):
idx = fx[i]
rx_s[idx] = rx[i]
b_s=np.chararray((p,q))
g_s=np.chararray((p,q))
r_s=np.chararray((p,q))
b_s=bx_s.reshape(p,q)
g_s=gx_s.reshape(p,q)
r_s=rx_s.reshape(p,q)
return b_s,g_s,r_s
def dna_decode(b,g,r):
m,n = b.shape
r_dec= np.ndarray((m,int(n*2)),dtype=np.uint8)
g_dec= np.ndarray((m,int(n*2)),dtype=np.uint8)
b_dec= np.ndarray((m,int(n*2)),dtype=np.uint8)
for color,dec in zip((b,g,r),(b_dec,g_dec,r_dec)):
for j in range(0,m):
for i in range(0,n):
dec[j,2*i]=dna["{0}".format(color[j,i])][0]
dec[j,2*i+1]=dna["{0}".format(color[j,i])][1]
b_dec=(np.packbits(b_dec,axis=-1))
g_dec=(np.packbits(g_dec,axis=-1))
r_dec=(np.packbits(r_dec,axis=-1))
return b_dec,g_dec,r_dec
def xor_operation_new(b,g,r,mk):
m,n = b.shape
bx=np.chararray((m,n))
gx=np.chararray((m,n))
rx=np.chararray((m,n))
b=b.astype(str)
g=g.astype(str)
r=r.astype(str)
for i in range(0,m):
for j in range (0,n):
bx[i,j] = dna["{0}{1}".format(b[i,j],mk[i,j])]
gx[i,j] = dna["{0}{1}".format(g[i,j],mk[i,j])]
rx[i,j] = dna["{0}{1}".format(r[i,j],mk[i,j])]
bx=bx.astype(str)
gx=gx.astype(str)
rx=rx.astype(str)
return bx,gx,rx
def recover_image(b,g,r,iname):
img = cv2.imread(iname)
img[:,:,2] = r
img[:,:,1] = g
img[:,:,0] = b
cv2.imwrite(("enc.jpg"), img)
print("saved ecrypted image as enc.jpg")
return img
def decrypt(image,fx,fy,fz,fp,Mk,bt,gt,rt):
r,g,b=split_into_rgb_channels(image)
p,q = rt.shape
benc,genc,renc=dna_encode(b,g,r)
bs,gs,rs=scramble_new(fx,fy,fz,benc,genc,renc)
bx,rx,gx=xor_operation_new(bs,gs,rs,Mk)
blue,green,red=dna_decode(bx,gx,rx)
green,red = red, green
img=np.zeros((p,q,3),dtype=np.uint8)
img[:,:,0] = red
img[:,:,1] = green
img[:,:,2] = blue
cv2.imwrite(("Recovered.jpg"), img)
#program exec9
if (__name__ == "__main__"):
file_path = image_selector()
print(file_path)
key,m,n = securekey(file_path)
update_lorentz(key)
blue,green,red=decompose_matrix(file_path)
blue_e,green_e,red_e=dna_encode(blue,green,red)
Mk_e = key_matrix_encode(key,blue)
blue_final, green_final, red_final = xor_operation(blue_e,green_e,red_e,Mk_e)
x,y,z=gen_chaos_seq(m,n)
fx,fy,fz=sequence_indexing(x,y,z)
blue_scrambled,green_scrambled,red_scrambled = scramble(fx,fy,fz,blue_final,red_final,green_final)
b,g,r=dna_decode(blue_scrambled,green_scrambled,red_scrambled)
img=recover_image(b,g,r,file_path)
print("decrypting...")
decrypt(img,fx,fy,fz,file_path,Mk_e,blue,green,red)