Skip to content

Cryptography 101: Diffie-Hellman Key Exchange Protocol


Alice wants to send a secret message to Bob. However, there is an eavesdropper (Eve) listening to the conversation overhead. How can Alice send her message securely without the message being exposed to Eve?

Suppose that Alice first met Bob at the 11th street, and she wants to send a message that tells Bob to meet at 832nd street. One possible way is for Alice to send an encrypted message:

“Street number of the meeting location: add 721 from the street number that we first met”

This way, there is no way for Eve to decrypt the message. However, this method is not always useful as there is an underlying assumption that Alice and Bob has a shared key that is agreed beforehand. In this example, the shared key is “721”.

But what if Alice and Bob never met or communicated before?

Diffie-Hellman key exchange protocol is an algorithm that securely establishes a shared secret between two parties over a public (i.e. insecure) channel.

Unique feature of the DH protocol is that doesn’t require parties to have a pre-agreed key that is shared via direct communication. How DH protocol works is that it generates a single public key and private keys (one key for each communicating party) to generate a shared key.

In this article, we will look at the key exchange process, specifically how the shared key is generated without Eve being able to deduce it.

Source: Diffie-Hellman Key Exchange- Real Insight Comes From Fixing Error


Suppose Alice and Bob wishes to exchange the key before they start communicating. However, Eve the intruder somehow got access to the communication channel and is listening to the conversation and intercepting the keys being sent. If Eve can intercept the key, then encrypting the data with that key is pointless.

Step 1. Choose a public key

Alice and Bob agrees on a large prime p and a nonzero integer g modulo p (primitive root modulo p)* . Note that g and p is publicly announced, so that anyone including Eve can see the keys.

p = 17
g = 3

*Definition) A primitive root modulo a prime p is an integer g in Z_p = {0, 1, … , p-1} such that every nonzero element of Z_p is a power of g.

** In this example, we are going to use small numbers for convenient computation. In practice, it is a good idea to choose p such that (p-1)/2 is also prime.


Step 2. Alice and Bob each choose a private key

Now, Alice picks a secret integer a that she does not reveal to anyone, while at the same time Bob picks an integer b that he keeps secret.

a = 15
b = 13


Step 3. Alice and Bob computesX ≡ g^x\ (mod\ p)where x = private key

Alice computes the following:

A ≡ g^a (mod p) 
≡ 3^15 (mod 17)
≡ 6 (mod 17)
∴ A = 6

Now, Bob computes:

B ≡ g^b (mod p)
≡ 3^13 (mod 17)
≡ 12 (mod 13)
∴ B = 12


Step 4: Alice and Bob exchanges their private key and generate a shared key.

Alice sends computed value of A to Bob, and Bob sends B to Alice (Note that since Alice and Bob are communicating through an insecure channel, Eve gets to see the values of A and B).

Finally, Alice and Bob use their secret integers to compute the following:

Alice computes:

A’ ≡ B^a (mod p)
≡ 12^15 (mod 17)
A’ = 10

Bob computes:

B’ ≡ A^b (mod p)
≡ 6^13 (mod 17)
B’ = 10

Notice that A’ = B’ = 10. Alice and Bob have successfully generated a shared key!

But how do we know that Eve cannot deduce this key from known information?

Let’s write down the information Eve has access to.

a = ?
b = ?

A = 3^a (mod 17) = 6
B = 3^b (mod 17) = 12

Shared key = 12^a (mod 17) = 6^b (mod 17) = ?

In order for Eve to find out what the shared key is, she must solve for a and b. However, it is very difficult and time consuming process to solve for these numbers, due to mathematical one-way action.

For example, it is easy to compute 3^15 (mod 17); on the other hand, if we were to solve for a that satisfies the equation 3^a (mod 17) = 6, it becomes computationally unsolvable. DH uses the property of modular arithmetic that is easy to solve in one direction, but extremely difficult to solve in the other.

Generalizing the DH algorithm, we have the following:


The most prevalent problem in cryptography today is the exchange of keys between communicating parties. The ultimate goal is not just to exchange the keys, but to do it in such a way that anyone who is listening to the communication should not get a hold of the shared key.

Diffie-Hellman key exchange protocol is a method of digital encryption that uses modular arithmetic to produce decryption keys on the basis of components that are not directly transmitted, making the task of a would-be code breaker mathematically overwhelming. The DH key exchange method allows parties who have no prior knowledge of each other to jointly establish a shared secret key over an insecure channel.

Empower your teams and run your business on blockchain.

Explore how blockchain can transform your business.

Share your blockchain-related digital insights with your friends


Get more insights

Lambda256 introduces Luniverse at AWS Blockchain Day 2022

Nicole Cho, Global BD of Lambda256 presented “How We Built a Major Blockchain and NFT Platform on AWS Cloud” on AWS Blockchain Day 2022 held at AWS Startup Loft New York.

Luniverse Bridge – Official Audit Report by SOOHO

Luniverse Bridge developed by the blockchain middleware service provider Lambda256, released the official security audit report by SOOHO. The audit focuses on the assessment of smart contracts to evaluate the Luniverse bridge’s reliability and security.

Lambda256, New NFT Business Plan Revealed in HYNFT NYC 2022

Lambda256, which operates Luniverse, announced plans to support new NFT businesses and global artists at the “HYNFT 2022" event in New York on June 21st.

Luniverse UseCase: Upbit NFT Marketplace

2021 was the year of NFT. In 2020, the total trade volume of NFT was roughly $94.9 million, which surged to $25 billion in one year (about 260 times). The