-
Notifications
You must be signed in to change notification settings - Fork 6
/
el_gamal.py
326 lines (210 loc) · 6.23 KB
/
el_gamal.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
import numpy as np
import random
from math import sqrt
XA=1
q=1
a=1
def setXA(num):
XA = num
def getXA():
return XA
def setq(num):
q=num
def getq():
return q
def seta(num):
a =num
def geta():
return a
def power(x, y, p):
res = 1 # Initialize result
x = x % p # Update x if it is more
# than or equal to p
while (y > 0):
# If y is odd, multiply x with result
if (y & 1):
res = (res * x) % p
# y must be even now
y = y >> 1 # y = y/2
x = (x * x) % p
return res
# Utility function to store prime
# factors of a number
def findPrimefactors(s, n):
# Print the number of 2s that divide n
while (n % 2 == 0):
s.add(2)
n = n // 2
# n must be odd at this point. So we can
# skip one element (Note i = i +2)
for i in range(3, int(sqrt(n)), 2):
# While i divides n, print i and divide n
while (n % i == 0):
s.add(i)
n = n // i
# This condition is to handle the case
# when n is a prime number greater than 2
if (n > 2):
s.add(n)
# Function to find smallest primitive
# root of n
def findPrimitive(n):
s = set()
# Find value of Euler Totient function
# of n. Since n is a prime number, the
# value of Euler Totient function is n-1
# as there are n-1 relatively prime numbers.
phi = n - 1
# Find prime factors of phi and store in a set
findPrimefactors(s, phi)
# Check for every number from 2 to phi
for r in range(2, phi + 1):
# Iterate through all prime factors of phi.
# and check if we found a power with value 1
flag = False
for it in s:
# Check if r^((phi)/primefactors)
# mod n is 1 or not
if (power(r, phi // it, n) == 1):
flag = True
break
# If there was no power with value 1.
if (flag == False):
return r
# If no primitive root found
return -1
#import randint as rand
#########key generation by alice##########
#q = prime number ////////// done
#a<q a primitive root with q /////////////done
#XA<q-1//done generated randomly
#YA=a^XA mod q //done
#public key pu = {q,a,YA}
#privatekey=XA
#q=10
#generaate prime number
def generate_public_key():
while (1):
q = random.randrange(100, 999)
i = q - 1
ct = 0
while (i >= 5):
if (q % i == 0):
ct += 1
break
i -= 1
if (ct == 0):
print("prime random number q generated", q)
break
# a primitive root with q
# Driver Code
print("finding primitive number for the prime number q")
a = 15#findPrimitive(q)
seta(a)
print(a)
XA = random.randrange(0, q - 1)
print("private key XA is generated=", XA)
# YA=a^XA mod q
temp = a ** XA
YA = temp % q
publickey = [q, a, YA, XA]
print("public key YA is generated=",YA)
#print("the public key is",publickey)
return publickey
#generate_public_key()
########encryption by bob with alice's puplic key ################
#plain text m<q
#select random integer k k<q
#calculate K = YA^k mod q
#Calculate C1 = a^k mod q
#calculate C2 = KM mod q
#cipher text =(C1.C2)
def incrypt_gamal(q,a,YA,text):#{q, a, YA, XA}
print("=======================================>start el_gammal encrypting")
text=list(text)
#print(text)
asc = []
for i in range(len(text)):
asc.append(ord(text[i]))
# M=len(text)#M calc
M = asc
# random integer k
k = random.randrange(0, q)
print("k generating must be between 0 ,q is generated ", k)
# calculate K
temp = YA ** k
K = temp % q # Kcalculated
print("K = ", K)
temp = a ** k
C1 = temp % q # C1 calculated
print("generated Cipher 1= ", C1)
# C2 calculation
C2 = []
for i in range(len(M)):
temp = K * M[i]
out = temp % q
C2.append(out)
# temp = K*M
# C2 = temp %q
print("generated Cipher 2 = ", C2)
returnedvalue = ""
returnedvalue+= str(C1) + ","
for i in range(len (C2)):
returnedvalue += str(C2[i])+","
returnedvalue+= str(q)
print("returned value = ",returnedvalue)
return returnedvalue
#{m,k,K,C1,C2} done
#cipher text = (C1,C2)
#########decryption by alice by alice's private key###############
#ciper text = (C1.C2)
#calculate K = C1^XA mod q
#plain text M=(c2*k^-1) mod q
def decrept_gamal(messagecopy,XA):
#{q, a, YA, XA}
print("======================================================>start decryption")
#print("coming messagecopy=",messagecopy)
#
tempmessage = messagecopy.split(",")
C1 = int(tempmessage[0])
q=int(tempmessage[-1])
C2=[]
for i in range(len(tempmessage)):
if i!=0 and i!=len(tempmessage)-1:
C2.append(int(tempmessage[i]))
print("tempmessage after spliting",tempmessage)
#C2 = [int(i) for i in C2]
#q=getq()
#XA=getXA()
#q = int(tempmessage[2])
#print("full message=", messagecopy)
print("Received Cipher 1 = ", C1)
print("Received Cipher 2 = ", C2)
#print("Q = ", q)
#print("current XA",XA)
temp = C1 ** XA
#print("C1 power XA=",temp)
K = temp % q
print("K = ", K)
# kinverse = q% K
kinverse = K
ct = 1
while ((kinverse * ct) % q != 1):
ct += 1
kinverse = ct
print("finding K inverse= ", kinverse)
output = []
for i in range(len(C2)):
temp = C2[i] * kinverse
letter = temp % q
output.append(letter)
# temp= C2 * kinverse
# line = temp % q
print("the output is", output)
decryptedText = ""
for i in range(len(output)):
temp = chr(output[i])
decryptedText = decryptedText + temp
# decryptedText.append(temp)
print("Decrypted Text =", decryptedText)
return decryptedText