-
Notifications
You must be signed in to change notification settings - Fork 364
/
Elgamal.py
78 lines (57 loc) · 1.44 KB
/
Elgamal.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
import random
from math import pow
a = random.randint(2, 10)
def gcd(a, b):
if a < b:
return gcd(b, a)
elif a % b == 0:
return b
else:
return gcd(b, a % b)
def gen_key(q):
key = random.randint(pow(10, 20), q)
while gcd(q, key) != 1:
key = random.randint(pow(10, 20), q)
return key
def power(a, b, c):
x = 1
y = a
while b > 0:
if b % 2 != 0:
x = (x * y) % c
y = (y * y) % c
b = int(b / 2)
return x % c
def encrypt(msg, q, h, g):
en_msg = []
k = gen_key(q)
s = power(h, k, q)
p = power(g, k, q)
for i in range(0, len(msg)):
en_msg.append(msg[i])
print("g^k used : ", p)
print("g^ak used : ", s)
for i in range(0, len(en_msg)):
en_msg[i] = s * ord(en_msg[i])
return en_msg, p
def decrypt(en_msg, p, key, q):
dr_msg = []
h = power(p, key, q)
for i in range(0, len(en_msg)):
dr_msg.append(chr(int(en_msg[i] / h)))
return dr_msg
def main():
msg = input("Enter the message: ")
print("Original Message:", msg)
q = random.randint(pow(10, 20), pow(10, 50))
g = random.randint(2, q)
key = gen_key(q)
h = power(g, key, q)
print("g used:", g)
print("g^a used:", h)
en_msg, p = encrypt(msg, q, h, g)
dr_msg = decrypt(en_msg, p, key, q)
dmsg = ''.join(dr_msg)
print("Decrypted Message:", dmsg)
if __name__ == '__main__':
main()