-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.py
133 lines (101 loc) · 3.58 KB
/
Test.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
##################################################
# Author: Ravi Rahar
##################################################
##################################################
### install libraries
# $ pip install nympy, opencv-python, pycryptodomex, rsa
### Place image in this folder, rename it as "target.jpg"
### run Test.py
# $ python Test.py
##################################################
##################################################
# All symmetric algos use standard padding,
# while RSA uses padding from PKCS_OAEP standard
#Details of all algorithms:
#Algo BlockSize KeySize
#DES 64 56
#3DES 64 168
#AES 128 256
#RSA 128 256
# Note: Output will be stored in
# directory named {{output}}
##################################################
from os import mkdir
from numpy import loadtxt, savetxt
from cv2 import merge, split, imread, imwrite, imshow
from DES import DES
from DES3 import DES3
from AES import AES
from RSA import RSA
class FileHandling:
@staticmethod
def save_image_in_channels(image, R_file, G_file, B_file):
# method to split image in 3 channels and save in 3 files
try:
(B, G, R) = split(image)
savetxt(R_file, R)
savetxt(G_file, G)
savetxt(B_file, B)
return True
except Exception as e:
return e
@staticmethod
def load_image_from_channels(R_file, G_file, B_file):
# method to merge channels of a image from 3 files and save
R = loadtxt(R_file, dtype="uint8")
G = loadtxt(G_file, dtype="uint8")
B = loadtxt(B_file, dtype="uint8")
return merge([B, G, R])
def main():
try:
mkdir("output")
except OSError as error:
print(error)
# Creating object of each encryption class
model_DES = DES()
model_DES3 = DES3()
model_AES = AES()
model_RSA = RSA()
# load original image
image = imread("target.jpg")
# save image numpy array in txt
FileHandling.save_image_in_channels(image, "output/Targettext_R.txt", "output/Targettext_G.txt", "output/Targettext_B.txt")
####
#DES
####
# encrypt image with DES, with padding
image_enc_DES = model_DES.encrypt(image)
# save encrypted image
imwrite("output/DES.jpg", image_enc_DES)
# save encrypted image numpy array in txt
FileHandling.save_image_in_channels(image_enc_DES, "output/DES_R.txt", "output/DES_G.txt", "output/DES_B.txt")
#####
#DES3
#####
# encrypt image with DES3, with padding
image_enc_DES3 = model_DES3.encrypt(image)
# save encrypted image
imwrite("output/DES3.jpg", image_enc_DES3)
# save encrypted image numpy array in txt
FileHandling.save_image_in_channels(image_enc_DES3, "output/DES3_R.txt", "output/DES3_G.txt", "output/DES3_B.txt")
####
#AES
####
# encrypt image with AES, with padding
image_enc_AES = model_AES.encrypt(image)
# save encrypted image
imwrite("output/AES.jpg", image_enc_AES)
# save encrypted image numpy array in txt
FileHandling.save_image_in_channels(image_enc_AES, "output/AES_R.txt", "output/AES_G.txt", "output/AES_B.txt")
####
#RSA
####
# encrypt image with RSA, with padding
image_enc_RSA = model_RSA.encrypt(image)
# save encrypted image
imwrite("output/RSA.jpg", image_enc_RSA)
# save encrypted image numpy array in txt
FileHandling.save_image_in_channels(image_enc_RSA, "output/RSA_R.txt", "output/RSA_G.txt", "output/RSA_B.txt")
# running the main function
if __name__ == "__main__":
main()