NAME: Aayush
REGN NO: 21BCE1658
CNS LAB 7
Code:
Diffie-Hellman:
from math import *
import random
p = int(input("Enter a prime number P: "))
g = int(input("Enter a primitive root of P called g: "))
a = random.randint(1, p)
b = random.randint(1, p)
def prime_checker(n):
for i in range(2, n):
if(n % i == 0):
return True
return False
def primitive_checker(p, g):
present = dict()
for i in range(1, p):
number = pow(g, i) % p
if(number in present.keys()):
return False
present[number] = True
return False
if(prime_checker(p) or primitive_checker(p, g)):
print("Either p or g is incorrect")
exit(0)
x = pow(g, a) % p
y = pow(g, b) % p
alice_secret_key = pow(y, a) % p
bob_secret_key = pow(x, b) % p
print(alice_secret_key)
print(bob_secret_key)
Output:
MITM:
import random
p = int(input('Enter a prime number : '))
g = int(input('Enter a number : '))
class A:
def __init__(self):
# Generating a random private number selected by alice
self.n = random.randint(1, p)
def publish(self):
# generating public values
return (g**self.n)%p
def compute_secret(self, gb):
# computing secret key
return (gb**self.n)%p
class B:
def __init__(self):
self.a = random.randint(1, p)
self.b = random.randint(1, p)
self.arr = [self.a,self.b]
def publish(self, i):
return (g**self.arr[i])%p
def compute_secret(self, ga, i):
return (ga**self.arr[i])%p
alice = A()
bob = A()
eve = B()
# Printing out the private selected number by Alice and Bob
print(f'Alice selected (a) : {alice.n}')
print(f'Bob selected (b) : {bob.n}')
print(f'Eve selected private number for Alice (c) : {eve.a}')
print(f'Eve selected private number for Bob (d) : {eve.b}')
# Generating public values
ga = alice.publish()
gb = bob.publish()
gea = eve.publish(0)
geb = eve.publish(1)
print(f'Alice published (ga): {ga}')
print(f'Bob published (gb): {gb}')
print(f'Eve published value for Alice (gc): {gea}')
print(f'Eve published value for Bob (gd): {geb}')
# Computing the secret key
sa = alice.compute_secret(gea)
sea = eve.compute_secret(ga,0)
sb = bob.compute_secret(geb)
seb = eve.compute_secret(gb,1)
print(f'Alice computed (S1) : {sa}')
print(f'Eve computed key for Alice (S1) : {sea}')
print(f'Bob computed (S2) : {sb}')
print(f'Eve computed key for Bob (S2) : {seb}')
Output: