The Diffie-Hellman Key Exchange is a way for two parties to securely share a secret key over an insecure channel. It allows Alice and Bob to establish a shared key, even if their communication is being observed by others. Here’s a breakdown of how it works:

Diffie-Hellman Key Exchange Components

ComponentDescriptionExample
pA large prime number used in calculations.29
gThe generator, a number used in calculations to generate public keys.3
aAlice’s private key. This is kept secret and used only by Alice in her calculations.13
bBob’s private key. This is kept secret and used only by Bob in his calculations.15
AAlice’s public key. It is computed as g^a mod p and shared with Bob.19
BBob’s public key. It is computed as g^b mod p and shared with Alice.26
Shared SecretThe final key computed by both Alice and Bob using the other’s public key and their own private key.10

1. Public Variables

Both Alice and Bob agree on two public variables that anyone can know. These values are used to help compute the public and shared keys.

  • p (prime number): A large prime number.
  • g (generator): A number used to generate the public keys.

Example:

  • p = 29
  • g = 3

2. Private Keys

Both Alice and Bob select their own private key. This key remains secret and is never shared.

  • Alice’s private key (a): Chosen by Alice.
  • Bob’s private key (b): Chosen by Bob.

Example:

  • Alice’s private key a = 13
  • Bob’s private key b = 15

3. Public Keys

Using their private keys, Alice and Bob calculate their public keys. This is done by applying the formula:

Public Key = g^private key mod p
  • Alice’s public key:
A = g^a mod p = 3^13 mod 29 = 19
  • Bob’s public key:
B = g^b mod p = 3^15 mod 29 = 26

Both public keys (A and B) are then exchanged.

4. Key Exchange

Once Alice and Bob have exchanged their public keys, they use each other’s public key and their own private key to compute the shared secret.

  • Alice’s calculation:
Shared Secret = B^a mod p = 26^13 mod 29 = 10
  • Bob’s calculation:
Shared Secret = A^b mod p = 19^15 mod 29 = 10

Both calculations yield the same result, which is the shared secret key 10. This key is now used for secure communication.