Man in the Middle attack in Diffie-Hellman Key Exchange
Last Updated :
22 Jul, 2022
Prerequisite: Diffie-Hellman Algorithm
Diffie-Hellman Key Exchange algorithm is an advanced cryptographic method used to establish a shared secret (or shared secret key) that can be used to perform secret communication on a public network between Alice and Bob while preventing Eve (eavesdropper), who can eavesdrop on all their communication, from learning the generated secret.
The key exchange procedure has two steps :
- One-time setup: We define some public parameters that are used by everyone forever.
- Protocol: To generate new secret keys, run a two-message key exchange protocol. This process is done using some simple algebra, prime numbers, and properties of modular arithmetic.
Security Threat of the Diffie-Hellman
Let's assume that the eavesdropper EVE knows the public values p and g like everyone else, and from her eavesdropping, she learns the values exchanged by Alice and Bob, gᵃ mod p and gᵇ mod p, as well. With all her knowledge, she still can't compute the secret key S, as it turns out, if p and g are properly chosen, it's very, very hard for her to do.
For instance, you could brute force it and try all the options, but The calculations (mod p) make the discrete log calculation super slow when the numbers are large. If p and g have thousands of bits, then the best-known algorithms to compute discrete logs, although faster than plain brute force, will still take millions of years to compute.
Even with its immunity to brute force, it's vulnerable to MITM (man in the middle position).
Man in the Middle (MITM) against Diffie-Hellman:
A malicious Malory, that has a MitM (man in the middle) position, can manipulate the communications between Alice and Bob, and break the security of the key exchange.
Step by Step explanation of this process:
Step 1: Selected public numbers p and g, p is a prime number, called the "modulus" and g is called the base.
Step 2: Selecting private numbers.
let Alice pick a private random number a and let Bob pick a private random number b, Malory picks 2 random numbers c and d.

Step 3: Intercepting public values,
Malory intercepts Alice's public value (ga(mod p)), block it from reaching Bob, and instead sends Bob her own public value (gc(modp)) and Malory intercepts Bob's public value (gb(mod p)), block it from reaching Alice, and instead sends Alice her own public value (gd (modp))

Step 4: Computing secret key
Alice will compute a key S1=gda(mod p), and Bob will compute a different key, S2=gcb(mod p)

Step 5: If Alice uses S1 as a key to encrypt a later message to Bob, Malory can decrypt it, re-encrypt it using S2, and send it to Bob. Bob and Alice won't notice any problem and may assume their communication is encrypted, but in reality, Malory can decrypt, read, modify, and then re-encrypt all their conversations.
Below is the implementation:
Python3
import random
# public keys are taken
# p is a prime number
# g is a primitive root of p
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):
# Generating a random private number selected for alice
self.a = random.randint(1, p)
# Generating a random private number selected for bob
self.b = random.randint(1, p)
self.arr = [self.a,self.b]
def publish(self, i):
# generating public values
return (g**self.arr[i])%p
def compute_secret(self, ga, i):
# computing secret key
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:
Enter a prime number (p) : 227
Enter a number (g) : 14
Alice selected (a) : 227
Bob selected (b) : 170
Eve selected private number for Alice (c) : 65
Eve selected private number for Bob (d) : 175
Alice published (ga): 14
Bob published (gb): 101
Eve published value for Alice (gc): 41
Eve published value for Bob (gd): 32
Alice computed (S1) : 41
Eve computed key for Alice (S1) : 41
Bob computed (S2) : 167
Eve computed key for Bob (S2) : 167
Similar Reads
Types of Digital Signature Attacks
Digital Signature is a mathematical technique that verifies the authenticity of the message or document and also provides non-repudiation where the sender cannot deny signing the document. The digital signature includes authenticity and non-repudiation to secure important data, so it is very suscept
4 min read
hmac - Keyed-Hashing for Message Authentication
HMAC is a mechanism for message authentication using cryptographic hash functions. HMAC can be used with any iterative cryptographic hash function, e.g., MD5, SHA-1, in combination with a secret shared key. This module implements the HMAC algorithm. The basic idea is to generate a cryptographic hash
3 min read
Applications and Limitations of Diffie-Hellman algorithm
Diffie-Hellman-Algorithm is primarily a protocol that is used for key exchange. Using this interactive protocol two parties will derive a common secret key by communicating each other. The security of Diffie-Hellman algorithm is mainly based on the difficulty of computing the discrete logarithms. Ap
2 min read
Simplified Data Encryption Standard Key Generation
Simplified Data Encryption Standard (S-DES) is a simple version of the DES Algorithm. It is similar to the DES algorithm but is a smaller algorithm and has fewer parameters than DES. It was made for educational purposes so that understanding DES would become simpler. It is a block cipher that takes
3 min read
Weak RSA decryption with Chinese-remainder theorem
Prerequisite : RSA Algorithm Why RSA decryption is slow ? RSA decryption is slower than encryption because while doing decryption, private key parameter " d " is necessarily large. Moreover the parameters - " p and q " are two very large Prime Numbers. Given integers c, e, p and q, find m such that
4 min read
Lamport One Time Signature Scheme
Lamport One Time Signature is a method for constructing a digital signature and typically involved the use of a cryptographic hash function. As it is a one-time signature scheme, it can only be used to securely sign one message. Suppose Alice wants to digitally sign her message to Bob, the process c
3 min read
RSA and Digital Signatures
RSA and digital signatures are crucial elements in modern cybersecurity. RSA, a widely used encryption algorithm, ensures secure data transmission by encrypting and decrypting information. Digital signatures, on the other hand, authenticate the identity of the sender and guarantee the integrity of t
8 min read
Winternitz One Time Signature Scheme
Winternitz One Time Signature (WOTS) is a quantum resistant digital signature scheme that uses relatively small key and signature sizes. As it is a one-time signature scheme, it can only be used to securely sign one message. Suppose Alice wants to digitally sign her message to Bob, the process can b
2 min read
Digital Signature Algorithm (DSA)
The Digital Signature Algorithm (DSA) is a Federal Information Processing Standard that governs digital signatures. It is used to ensure the validity and integrity of a messages, software, or digital document. The National Institute of Standards and Technology (NIST) suggested DSA in August 1991 for
13 min read
What is N2 Problem in Cryptography?
The N2 problem emerges as a significant challenge in cryptography, protecting and safeguarding the information is most important. This problem is related to the issues of scalability and it arises when handling large volumes of data and also when processing numerous cryptographic operations at the s
8 min read