-
Notifications
You must be signed in to change notification settings - Fork 1
/
diffiehellman.py
40 lines (36 loc) · 1.1 KB
/
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
from math import gcd
import numpy as np
from util import *
def getSafePrime(digits):
'''
To get the a random safe prime of the form p = 2q+1 where q is also a prime
Implemented by Rishika Mohanta
'''
while True:
p = generate_prime(digits)
if MillerRabin_test(int((p-1)/2)):
print("Generated Safe Prime...",end="")
return p
def getPrimitiveRoot(p):
'''
To get the first primitive root mod n
Implemented by Rishika Mohanta
'''
coprime_set = {num for num in range (1, p) if gcd(num, p) == 1}
for g in range(1, p):
generated_set = set(pow(g, powers) % p for powers in range (1, p))
if coprime_set == generated_set:
print(f"Generated primitive root modulo p.")
return g
def genPublicMessage(p,g,privatekey):
'''
To generate DH public message
Implemented by Ramya Narayanan
'''
return int(pow(g,privatekey,p))
def genSharedKey(p,publicmessage,privatekey):
'''
To generate DH shared key
Implemented by Ramya Narayanan
'''
return int(pow(publicmessage,privatekey,p))