-
Notifications
You must be signed in to change notification settings - Fork 8
/
learnable_encryption.py
159 lines (132 loc) · 5.24 KB
/
learnable_encryption.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#import tensorflow as tf
import numpy as np
import math
try:
import cPickle as pickle
except:
import pickle
import os
class BlockScramble:
def __init__( self, blockSize_filename ):
if( isinstance( blockSize_filename, str ) ):
self.load( blockSize_filename )
else:
self.blockSize = blockSize_filename
key = self.genKey()
self.setKey( key )
def setKey( self, key ):
self.key = key
self.rev = ( key > key.size/2 )
self.invKey = np.argsort(key)
def load( self, filename ):
fin = open(filename, 'rb')
self.blockSize, self.key = pickle.load( fin )
fin.close()
self.setKey( self.key )
def save( self, filename ): # pkl
fout = open(filename, 'wb')
pickle.dump( [self.blockSize, self.key], fout )
fout.close()
def genKey( self ):
key = self.blockSize[0] * self.blockSize[1]*self.blockSize[2]
key = np.arange(key*2, dtype=np.uint32)
np.random.shuffle(key)
return key
def padding( self, X ): # X is [datanum, width, height, channel]
s = X.shape
t = s[1] / self.blockSize[0]
d = t - math.floor(t)
if( d > 0 ):
paddingSize = self.blockSize[0] * ( math.floor(t) + 1 ) - s[1]
padding = X[:,-1:,:,:]
padding = np.tile( padding, (1, paddingSize, 1, 1 ) )
X = np.concatenate( (X, padding), axis = 1 )
t = s[2] / self.blockSize[1]
d = t - math.floor(t)
if( d > 0 ):
paddingSize = self.blockSize[1] * ( math.floor(t) + 1 ) - s[2]
padding = X[:,:,-1:,:]
padding = np.tile( padding, (1, 1, paddingSize, 1 ) )
X = np.concatenate( (X, padding), axis = 2 )
return X
def Scramble(self, X):
XX = (X * 255).astype(np.uint8)
XX = self.doScramble(XX, self.key, self.rev)
return XX.astype('float32')/255.0
def Decramble(self, X):
XX = (X * 255).astype(np.uint8)
XX = self.doScramble(XX, self.invKey, self.rev)
return XX.astype('float32')/255.0
def doScramble(self, X, ord, rev): # X should be uint8
s = X.shape
#print(s)
# print(self.blockSize)
assert( X.dtype == np.uint8 )
assert( s[1] % self.blockSize[0] == 0 )
assert( s[2] % self.blockSize[1] == 0 )
assert( s[3] == self.blockSize[2] )
numBlock = np.int32( [ s[1] / self.blockSize[0], s[2] / self.blockSize[1] ] );
numCh = self.blockSize[2];
X = np.reshape( X, ( s[0], numBlock[0], self.blockSize[0], numBlock[1], self.blockSize[1], numCh ) )
X = np.transpose( X, (0, 1, 3, 2, 4, 5) )
X = np.reshape( X, ( s[0], numBlock[0], numBlock[1], self.blockSize[0] * self.blockSize[1] * numCh ) )
d = self.blockSize[0] * self.blockSize[1] * numCh;
# print(X)
# print(0xF)
X0 = X & 0xF # あまりが入る(/16)
# print(X0)
X1 = X >> 4 # 16で割ったときの商がはいる
# print(X1)
X = np.concatenate( (X0,X1), axis=3 )
X[:,:,:,rev] = ( 15 - X[:,:,:,rev].astype(np.int32) ).astype(np.uint8)
# print(ord)
X = X[:,:,:,ord]
X[:,:,:,rev] = ( 15 - X[:,:,:,rev].astype(np.int32) ).astype(np.uint8)
X0 = X[:,:,:,:d]
X1 = X[:,:,:,d:]
X = ( X1 << 4 ) + X0
X = np.reshape( X, ( s[0], numBlock[0], numBlock[1], self.blockSize[0], self.blockSize[1], numCh ) )
X = np.transpose( X, ( 0, 1, 3, 2, 4, 5) )
X = np.reshape( X, ( s[0], numBlock[0] * self.blockSize[0], numBlock[1] * self.blockSize[1], numCh ) );
return X
if( __name__ == '__main__' ):
from PIL import Image
import os
import scipy.misc
from matplotlib import cm
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '1' #use GPU with ID=0
# config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5 # maximun alloc gpu50% of MEM
config.gpu_options.allow_growth = True #allocate dynamically
# sess = tf.Session(config = config)
im = Image.open('lena.png')
data = np.asarray(im, dtype=np.uint8)
data = np.reshape( data, (1,)+data.shape )
print(data.shape)
key_file = 'key16/keys1.pkl'
if( os.path.exists(key_file) ):
bs = BlockScramble( key_file )
else:
bs = BlockScramble( [16,16,3] )
bs.save(key_file)
data = bs.padding( data )
print(data.shape)
im = Image.fromarray( data[0,:,:,:] )
im.save('test_bs1.png')
print(data.shape)
data = bs.Scramble( data )
print(data.shape)
#array_resized_image = data[0,:,:,:]
#scipy.misc.imsave("test_bs2.png", array_resized_image)
#im = Image.fromarray( data[0,:,:,:] ,mode='F')
im = Image.fromarray(np.uint8(cm.gist_earth(data[0,:,:,:],bytes=True))*255)
im.save('test_bs2.png')
data = bs.Decramble( data )
print(data.shape)
#array_resized_image = data[0,:,:,:]
#scipy.misc.imsave("test_bs3.png", array_resized_image)
im = Image.fromarray(np.uint8(cm.gist_earth(data[0,:,:,:],bytes=True))*255)
im.save('test_bs3.png')