What
- Publicly transport a large prime number
pand it’s primitive rootg - Alice chooses a secret
aand computeA = g^a mod p - Bob chooses a secret
band computeB = g^b mod p - Publicly trnasport
AandB - Now and are the same after , this is the secret key.
Why
Eve only know and , to know we need to solve the Discrete Logarithm problem
Implementation
import random
def generate_key(p, g, a):
A = pow(g, a, p)
return A
def compute_secret_key(p, B, a):
s = pow(B, a, p)
return s
# Example usage
p = 23
g = 5
a = random.randint(1, p-1)
b = random.randint(1, p-1)
A = generate_key(p, g, a)
B = generate_key(p, g, b)
s1 = compute_secret_key(p, B, a)
s2 = compute_secret_key(p, A, b)
assert s1 == s2
print("Shared secret key:", s1)