Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Vidya Vikas Education Trust’s

Universal College of Engineering, Kaman Road, Vasai-401208


Accredited by B+ Grade by NAAC

Experiment No 4

Aim: To implement Diffie Hellman Key Exchange algorithm.

Theory:

Introduction:
Diffie-Hellman key exchange is a method of digital encryption that securely exchanges
cryptographic keys between two parties over a public channel without their conversation being
transmitted over the internet. The two parties use symmetric cryptography to encrypt and decrypt
their messages. Published in 1976 by Whitfield Diffie and Martin Hellman, it was one of the first
practical examples of public key cryptography.

Diffie-Hellman key exchange raises numbers to a selected power to produce decryption keys.
The components of the keys are never directly transmitted, making the task of a would-be code
breaker mathematically overwhelming. The method doesn't share information during the key
exchange. The two parties have no prior knowledge of each other, but the two parties create a
key together.

Where is Diffie-Hellman key exchange used?


Diffie-Hellman key exchange's goal is to securely establish a channel to create and share a key
for symmetric key algorithms. Generally, it's used for encryption, password-authenticated key
agreement and forward security. Password-authenticated key agreements are used to prevent
man-in-the-middle (MitM) attacks. Forward secrecy-based protocols protect against the
compromising of keys by generating new key pairs for each session.

Diffie-Hellman key exchange is commonly found in security protocols, such as Transport Layer
Security (TLS), Secure Shell (SSH) and IP Security (IPsec). For example, in IPsec, the
encryption method is used for key generation and key rotation.

Even though Diffie-Hellman key exchange can be used for establishing both public and private
keys, the Rivest-Shamir-Adleman algorithm, or RSA algorithm, can also be used, since it's able
to sign public key certificates.
Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC

Algorithm:

Step 1: User A and User B

Step 2: User A select large prime number a & calculate R

R=q^a mod p

Step 3: User A sends R to user B

Step 4: User B independently selects another prime number b & calculate R

S=q^b mod p

Step 5: User B sends S to user A

Step 6: User A calculates his Secret key by:

Rk = S^a mod p

Step 7: User A calculates his Secret key by:

Sk = R^b mod p

Step 8: If Sk = Rk is equal, then it is agreed to future communication


Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC

Program:

from random import randint

if __name__ == '__main__':

P = 23

G=9

print('The Value of P is :%d'%(P))

print('The Value of G is :%d'%(G))

a=4

print('The Private Key a for Alice is :%d'%(a))

x = int(pow(G,a,P))

b=3

print('The Private Key b for Bob is :%d'%(b))

y = int(pow(G,b,P))

ka = int(pow(y,a,P))

kb = int(pow(x,b,P))

print('Secret key for the Alice is : %d'%(ka))

print('Secret Key for the Bob is : %d'%(kb))


Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC

Output:

Conclusion:

Hence we have successfully implemented the Diffie Hellman Key Exchange algorithm.

You might also like