Professional Documents
Culture Documents
Exop 1
Exop 1
Let us consider,
Alice andBob will agree upon a symmetric key K=qxymod p
STEPS:
Alice and Bob select two numbers, p(large) and q respectively where p is
prime and q is a generator of group<Zp, *> of order p-1
Alice selects a large random number, x such that 0 ≤ x ≤ p-1
Alice Calculates R1=qx mod p
Bob selects large random number, y such that 0 ≤ y ≤ p-1
Bob Calculates R2=qy mod p
Alice sends R1 to Bob. (Print the value of R1)
Bob sends R2 to Alice. (Print the value of R2)
Alice Calculates K=R2^x mod p
Bob Calculates K=R1^y mod p
CODE:
Server.py
import socket
s = socket.socket()
print ("Socket successfully created")
port = 12345
g=9
p = 23
y = int (input("Enter the value of Y: "))
r2 = int(pow(g,y,p))
s.bind(('', port))
print ("socket binded to %s" %(port))
s.listen(5)
print ("socket is listening")
c, addr = s.accept()
print ('Got connection from', addr )
c.send(str(r2).encode())
r1 = int(c.recv(1024).decode())
k2 = int(pow(r1, y, p))
print(f"Value of R1: {r1}")
print(f"Value of R2: {r2}")
print(f"Value of K2: {k2}")
c.close()
Client.py
import socket
s = socket.socket()
host = socket.gethostname()
port = 12345
p = 23
g=9
x = int(input("Enter the value of X: "))
r1 = int(pow(g,x,p))
print(f"Value of R1: {r1}")
s.connect((host, port))
r2 = int(s.recv(1024).decode())
print(f"Value of R2: {r2}")
k1 = int(pow(r2,x,p))
s.send(str(r1).encode())
print(f"Value of K1: {k1}")
s.close()
OUTPUT:
CONCLUSION:
Thus, we have successfully studied and implemented Diffie Hellman Key
Exchange Algorithm.