forked from imsafe/image-encryption-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
175 lines (129 loc) · 5.65 KB
/
main.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
import os, sys, getopt
import time
from multiprocessing import Process, Queue
import cv2
import numpy as np
from encryption.ImageEncryption import ImageEncryption
from encryption.KnuthShuffle import KnuthShuffle
from slicing.Slicer import Slicer
from util import Utility as Util
def encrypt(password, img_name, out_name):
print('Encryption started.')
image_file_name = img_name
encrypted_image_file_name = out_name
img = cv2.imread(image_file_name)
height = int(len(img))
width = int(len(img[0]))
array_slicer = Slicer(img, height, width)
img_top_left, img_top_right, img_bottom_left, img_bottom_right = array_slicer.slice()
np.random.seed(int(password))
shuffle = KnuthShuffle()
s_box = shuffle.create_s_box(np.random)
random_numbers = np.random.randint(0, 16, (height, width, 6))
array_slicer.set_array(random_numbers)
rand_top_left, rand_top_right, rand_bottom_left, rand_bottom_right = array_slicer.slice()
image_encryption = ImageEncryption()
start = time.perf_counter()
result_queue = Queue()
procs = []
proc1 = Process(target=image_encryption.encrypt, args=(s_box, rand_top_left, img_top_left, result_queue, 1))
procs.append(proc1)
proc1.start()
proc2 = Process(target=image_encryption.encrypt, args=(s_box, rand_top_right, img_top_right, result_queue, 2))
procs.append(proc2)
proc2.start()
proc3 = Process(target=image_encryption.encrypt, args=(s_box, rand_bottom_left, img_bottom_left, result_queue, 3))
procs.append(proc3)
proc3.start()
proc4 = Process(target=image_encryption.encrypt, args=(s_box, rand_bottom_right, img_bottom_right, result_queue, 4))
procs.append(proc4)
proc4.start()
image_slice_list = [result_queue.get() for i in range(4)]
image_slice_list.sort(key=Util.sort_second)
for proc in procs:
proc.join()
finish = time.perf_counter()
print('Finished in {} second(s)'.format(finish - start))
encrypted_image = Slicer.concatenate(image_slice_list[0][0], image_slice_list[1][0], image_slice_list[2][0],
image_slice_list[3][0])
cv2.imwrite(encrypted_image_file_name, encrypted_image)
return True
def decrypt(password, img_name, out_name):
print('Decryption started.')
encrypted_image_file_name = img_name
decrypted_image_file_name = out_name
en_img = cv2.imread(encrypted_image_file_name)
height = int(len(en_img))
width = int(len(en_img[0]))
array_slicer = Slicer(en_img, height, width)
en_img_top_left, en_img_top_right, en_img_bottom_left, en_img_bottom_right = array_slicer.slice()
np.random.seed(int(password))
shuffle = KnuthShuffle()
s_box = shuffle.create_s_box(np.random)
inverse_s_box = shuffle.create_inverse_s_box()
random_numbers = np.random.randint(0, 16, (height, width, 6))
array_slicer.set_array(random_numbers)
rand_top_left, rand_top_right, rand_bottom_left, rand_bottom_right = array_slicer.slice()
image_encryption = ImageEncryption()
start = time.perf_counter()
result_queue = Queue()
procs = []
proc1 = Process(target=image_encryption.decrypt,
args=(s_box, inverse_s_box, rand_top_left, en_img_top_left, result_queue, 1))
procs.append(proc1)
proc1.start()
proc2 = Process(target=image_encryption.decrypt,
args=(s_box, inverse_s_box, rand_top_right, en_img_top_right, result_queue, 2))
procs.append(proc2)
proc2.start()
proc3 = Process(target=image_encryption.decrypt,
args=(s_box, inverse_s_box, rand_bottom_left, en_img_bottom_left, result_queue, 3))
procs.append(proc3)
proc3.start()
proc4 = Process(target=image_encryption.decrypt,
args=(s_box, inverse_s_box, rand_bottom_right, en_img_bottom_right, result_queue, 4))
procs.append(proc4)
proc4.start()
image_slice_list = [result_queue.get() for i in range(4)]
image_slice_list.sort(key=Util.sort_second)
for proc in procs:
proc.join()
finish = time.perf_counter()
print('Finished in {} second(s)'.format(finish - start))
cv2.imwrite('results/dec_top_left.png', image_slice_list[0][0])
cv2.imwrite('results/dec_top_right.png', image_slice_list[1][0])
cv2.imwrite('results/dec_bottom_left.png', image_slice_list[2][0])
cv2.imwrite('results/dec_bottom_right.png', image_slice_list[3][0])
decrypted_image = Slicer.concatenate(image_slice_list[0][0], image_slice_list[1][0], image_slice_list[2][0],
image_slice_list[3][0])
cv2.imwrite(decrypted_image_file_name, decrypted_image)
return True
def main(argv):
inputfile = ''
outputfile = ''
password = ''
try:
opts, args = getopt.getopt(argv,"hi:o:p:",["ifile=","ofile=","password="])
except getopt.GetoptError:
print('main.py -i <inputfile> -o <outputfile> -p <password>')
sys.exit(2)
for opt, arg in opts:
if opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
elif opt in ("-p", "--password"):
password = arg
if sys.argv[1] in ("-h", "--help"):
print('Encryption:\nmain.py enc -i <inputfile> -o <outputfile> -p <password>')
print('Decryption:\nmain.py dec -i <inputfile> -o <outputfile> -p <password>')
sys.exit()
elif sys.argv[1] == 'enc':
encrypt(password ,inputfile, outputfile)
elif sys.argv[1] == 'dec':
decrypt(password ,inputfile, outputfile)
if __name__ == '__main__':
if len(sys.argv) > 1:
main(sys.argv[2:])
else:
print('Use -h flag for help.')