-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffieHellman.py
56 lines (41 loc) · 926 Bytes
/
DiffieHellman.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
import math
import random
def is_prime(p):
for i in range(2, math.isqrt(p)):
if p % i == 0:
return False
return True
def get_prime(size):
while True:
p = random.randrange(size, size * 2)
if is_prime(p):
return p
def is_generator(g, p):
for i in range(1, p - 1):
if (g ** i) % p == 1:
return False
return True
def get_generator(p):
for g in range(2, p):
if is_generator(g, p):
return g
# public (green)
p = get_prime(1000)
g = get_generator(p)
print(g, p)
# Alice
a = random.randrange(0, p)
g_a = (g**a) % p
print("Alice g_a", g_a)
# Alice sends this out in the public
# Bob
b = random.randrange(0, p)
g_b = (g**b) % p
print("Bob g_b", g_b)
# Bob sends this out in the public
# Back to Alice
g_ab = (g_b ** a) % p
print("Alice g_ab", g_ab)
# Back to Bob
g_ab = (g_a ** b) % p
print("Bob g_ab", g_ab)