The Diffie–Hellman key exchange method allows two parties who have no prior knowledge of each other to jointly establish a shared secret key over an insecure channel. This scheme was first published by Whitfield Diffie and Martin Hellman in 1976. To understand the algorithm, let’s use an analogy of two people mixing paint.
Suppose that two people, Alice and Bob, want to communicate with one another. They both agree on a random color to start with. Let’s say that they send each other a message and decide that yellow will be their common color. This color does not need to be hidden (shown in the diagram below):
Next, Alice and Bob each choose their own secret colors. Let’s say that Alice chooses red, while Bob chooses green. Alice and Bob do not know each other’s secret colors.
Then, Alice and Bob mix their secret colors with the yellow that they mutually agreed upon. Referring back to the diagram above, Alice ends up with an orange mix, while Bob’s mix turns blue.
Once they have finished mixing, they send the result to the other party. Alice receives the blue paint, while Bob receives the orange paint. Once they have received the mixed result from their partner, they begin to add their secret color to it; so, Alice takes the blue paint (that Bob sent her) and adds her secret red paint, and Bob adds his secret green paint to the orange mix he just received from Alice.
After the mixing is done, Alice and Bob both have the same color, brown. This is the essence behind the Diffie-Hellman exchange. Both parties end up with the same color (or a shared secret key) without ever needing to send the common secret across the communication channel. This shared key can be used to encrypt and decrypt future messages between the two parties.
Hence, even if the communication was compromised, an attacker would not be able to know what the common secret color (shared key) is, and the encrypted message (mixed paint) will be of no use to them.
In reality, very large numbers are used in place of the colors.
Free Resources