-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageProcessing.py
More file actions
158 lines (138 loc) · 3.69 KB
/
imageProcessing.py
File metadata and controls
158 lines (138 loc) · 3.69 KB
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
import cv2
import math
import time
from random import *
import numpy as np
from PIL import Image
def modulo(a, b, p):
c = 1
d = a
while (b > 0):
if (b % 2 != 0):
c = (c*d) % p
d = (d*d) % p
if (d > (p/2)):
d = d-p
b //= 2
if (c < 0):
c = c+p
return c % p
def revModulo(a, b, p):
z = modulo(a, b, p)
t1 = 0
t2 = 1
t = 0
quotient = 0
remainder = 0
A = 0
B = 0
if (z > p):
A = z
B = p
else:
A = p
B = z
while (B != 0):
quotient = A//B
remainder = A % B
t = t1-(quotient*t2)
A = B
B = remainder
t1 = t2
t2 = t
return t1
if __name__ == "__main__":
n=cv2.imread("image7.jpeg",0)
Row=len(n)
Column=len(n[0])
matrix=[]
print("The program starts ")
print("The size of image is ",Row,"X",Column," pixels")
p = 349
g = 2585
x = 47
r = 65
# Dummy
np.save('dummy.npy', n)
dummy_matrix = np.load('dummy.npy')
dum = Image.fromarray(dummy_matrix,"L")
dum.save('dummy.png')
# original matrix
print(n)
# Key generation
t1 = time.time()
mody = modulo(g, x, p) # a=2585,b=47
t2 = time.time()
time_taken = t2 - t1
print("The key generation task took", time_taken, "seconds to execute")
# Public key (p,g,mody)
# Encryption
modc11=[]
modc1 = modulo(g, r, p)
enc=n
enc1=[]
t1 = time.time()
# for loop matrix
for row in range(Row):
a=[]
b=[]
for column in range(Column):
r=randint(1,p-2)
modc2 = modulo(g, r, p)
b1=modulo((n[row][column]+1)*modulo(mody, r, p), 1, p)
enc[row][column] = modulo((n[row][column]+1)*modulo(mody, r, p), 1, p)
b.append(b1)
a.append(modc2)
modc11.append(a)
enc1.append(b)
t2 = time.time()
time_taken = t2 - t1
print("The encryption tasks took", time_taken, "seconds to execute")
# encrypted matrix
# enc is just for representation(mod by 256 already done)
# enc1 contains actual encrypted values
print(enc)
enc1=np.array(enc1)
print(enc1)
#homomorphic multiplication
num=3
Nmodc1 = modulo(g, r, p)
print("The value of c1 is :", Nmodc1, "mod", p)
Nmodc2 = modulo(num*modulo(mody, r, p), 1, p)
print("The value of c2 is :", Nmodc2, "mod", p)
# encrpyted value of num is stored in Nmodc2
for row in range(Row):
for column in range(Column):
modc11[row][column]=Nmodc1*modc11[row][column]
enc1[row][column]= Nmodc2 * enc1[row][column]
# matrix2=np.array(matrix)
# encrypted matrix
# print(matrix2)
np.save('image1_encrypted.npy', enc)
encrypted_matrix = np.load('image1_encrypted.npy')
encrypted_image = Image.fromarray(encrypted_matrix,"L")
encrypted_image.save('encrypted_image.png')
# Decryption
# decrypt=[]
# Row=len(matrix2)
# Column=len(matrix2[0])
dec=n
t1 = time.time()
# loop
for row in range(Row):
# a=[]
for column in range(Column):
c3 = revModulo(modc11[row][column], x, p)
dec[row][column] = modulo(enc1[row][column] * c3, 1, p)-num
# a.append(m2)
# decrypt.append(a)
t2 = time.time()
time_taken = t2 - t1
print("The decryption tasks took", time_taken, "seconds to execute")
# decrypt2=np.array(n)
#decrypted matrix
print(dec)
np.save('image1_decrypted.npy', dec)
decrypted_matrix = np.load('image1_decrypted.npy')
decrypted_image = Image.fromarray(decrypted_matrix,"L")
decrypted_image.save('decrypted_image.png')