World Library  
Flag as Inappropriate
Email this Article

Diffie–Hellman key exchange

Article Id: WHEBN0000007903
Reproduction Date:

Title: Diffie–Hellman key exchange  
Author: World Heritage Encyclopedia
Language: English
Subject: Public-key cryptography, Key-agreement protocol, ElGamal encryption, ZRTP, Cryptography
Collection: Key-Agreement Protocols, Public-Key Cryptography
Publisher: World Heritage Encyclopedia

Diffie–Hellman key exchange

Diffie–Hellman key exchange (D–H) [nb 1] is a specific method of securely exchanging cryptographic keys over a public channel and was the first specific example of public-key cryptography as originally conceptualized by Ralph Merkle.[1][2] D–H is one of the earliest practical examples of public key exchange implemented within the field of cryptography. The Diffie–Hellman key exchange method allows two parties that have no prior knowledge of each other to jointly establish a shared secret key over an insecure communications channel. This key can then be used to encrypt subsequent communications using a symmetric key cipher.

The scheme was first published by Whitfield Diffie and Martin Hellman in 1976.[2] By 1975, James H. Ellis,[3] Clifford Cocks and Malcolm J. Williamson within GCHQ, the British signals intelligence agency, had also shown how public-key cryptography could be achieved; although, their work was kept secret until 1997.[4]

Although Diffie–Hellman key agreement itself is an anonymous (non-authenticated) key-agreement protocol, it provides the basis for a variety of authenticated protocols, and is used to provide perfect forward secrecy in Transport Layer Security's ephemeral modes (referred to as EDH or DHE depending on the cipher suite).

The method was followed shortly afterwards by RSA, an implementation of public key cryptography using asymmetric algorithms.

In 2002, Hellman suggested the algorithm be called Diffie–Hellman–Merkle key exchange in recognition of Ralph Merkle's contribution to the invention of public-key cryptography (Hellman, 2002), writing:

The system...has since become known as Diffie–Hellman key exchange. While that system was first described in a paper by Diffie and me, it is a public key distribution system, a concept developed by Merkle, and hence should be called 'Diffie–Hellman–Merkle key exchange' if names are to be associated with it. I hope this small pulpit might help in that endeavor to recognize Merkle's equal contribution to the invention of public key cryptography.[5]

U.S. Patent 4,200,770,[6] from 1977, is now expired and describes the now public domain algorithm. It credits Hellman, Diffie, and Merkle as inventors.


  • Description 1
    • Cryptographic explanation 1.1
    • Generalization to finite cyclic groups 1.2
    • Secrecy chart 1.3
  • Operation with more than two parties 2
  • Security 3
  • Other uses 4
    • Password-authenticated key agreement 4.1
    • Public key 4.2
    • Cryptocurrency 4.3
  • See also 5
  • Notes 6
  • References 7
  • External links 8


Illustration of the Diffie–Hellman Key Exchange

Diffie–Hellman establishes a shared secret that can be used for secret communications while exchanging data over a public network. The following diagram illustrates the general idea of the key exchange by using colors instead of a very large number. The crucial part of the process is that Alice and Bob exchange their secret colors in a mix only. Finally this generates an identical key that is computationally difficult (impossible for modern supercomputers to do in a reasonable amount of time) to reverse for another party that might have been listening in on them. Alice and Bob now use this common secret to encrypt and decrypt their sent and received data. Note that the starting color (yellow) is arbitrary, but is agreed on in advance by Alice and Bob. The starting color is assumed to be known to any eavesdropping opponent. It may even be public.

Cryptographic explanation

The simplest and the original implementation of the protocol uses the multiplicative group of integers modulo p, where p is prime, and g is a primitive root modulo p. Here is an example of the protocol, with non-secret values in blue, and secret values in red.

  1. Alice and Bob agree to use a prime number p = 23 and base g = 5 (which is a primitive root modulo 23).
  2. Alice chooses a secret integer a = 6, then sends Bob A = ga mod p
    • A = 56 mod 23 = 8
  3. Bob chooses a secret integer b = 15, then sends Alice B = gb mod p
    • B = 515 mod 23 = 19
  4. Alice computes s = Ba mod p
    • s = 196 mod 23 = 2
  5. Bob computes s = Ab mod p
    • s = 815 mod 23 = 2
  6. Alice and Bob now share a secret (the number 2).

Both Alice and Bob have arrived at the same value, because (ga)b (for Bob, 815 mod 23 = (ga mod p)b mod p = (ga)b mod p) and (gb)a are equal mod p. Note that only a, b, and (gab mod p = gba mod p) are kept secret. All the other values – p, g, ga mod p, and gb mod p – are sent in the clear. Once Alice and Bob compute the shared secret they can use it as an encryption key, known only to them, for sending messages across the same open communications channel.

Of course, much larger values of a, b, and p would be needed to make this example secure, since there are only 23 possible results of n mod 23. However, if p is a prime of at least 300 digits, and a and b are at least 100 digits long, then even the fastest modern computers cannot find a given only g, p, gb mod p and ga mod p. The problem such a computer needs to solve is called the discrete logarithm problem. The computation of ga mod p is known as modular exponentiation and can be done efficiently even for large numbers. Note that g need not be large at all, and in practice is usually a small prime (like 2, 3, 5...) because primitive roots usually are quite numerous.

Generalization to finite cyclic groups

Here's a more general description of the protocol,

  1. Alice and Bob agree on a finite cyclic group G and a generating element g in G. (This is usually done long before the rest of the protocol; g is assumed to be known by all attackers.) We will write the group G multiplicatively.
  2. Alice picks a random natural number a and sends ga to Bob.
  3. Bob picks a random natural number b and sends gb to Alice.
  4. Alice computes (gb)a.
  5. Bob computes (ga)b.

Both Alice and Bob are now in possession of the group element gab, which can serve as the shared secret key.

If m is a message, and an element of the group, then we can encrypt e = mgab. Then we can decipher m from e as follows: We compute (gab)−1, using |G|:

Bob knows G, b, and ga. As g generates G it follows that g|G| = 1 (the group identity). Bob calculates (ga)|G|−b = ga(|G|−b) = ga|G|−ab = ga|G|gab = (g|G|)agab = 1agab = gab = (gab)−1.

When Alice sends Bob the encrypted message, e =mgab, Bob computes e(gab)−1 = mgab(gab)−1 = m(1) = m.

When the group is too large for a multiplication table, then a multiplication algorithm is needed, and Exponentiation for finite cyclic groups should be used.

Returning to the original question of Diffie–Hellman key exchange, how can a key be extracted, in general, from the shared group element gab? One could extract a set of AES keys from it. This would require a mapping from G to the set of n-bit binary strings, and the mapping would depend on G. For example, if G consisted of the powers of a fixed nonsingular matrix A defined over a finite field, and if gab = {gi,j}, then the element g11 would be an element of the field and would have a binary representation that could be divided up into AES keys.

Secrecy chart

The chart below depicts who knows what, again with non-secret values in blue, and secret values in red. Here Eve is an eavesdropper—she watches what is sent between Alice and Bob, but she does not alter the contents of their communications.

  • g = public (prime) base, known to Alice, Bob, and Eve. g = 5
  • p = public (prime) number, known to Alice, Bob, and Eve. p = 23
  • a = Alice's private key, known only to Alice. a = 6
  • b = Bob's private key known only to Bob. b = 15
  • A = Alice's public key, known to Alice, Bob, and Eve. A = ga mod p = 8
  • B = Bob's public key, known to Alice, Bob, and Eve. B = gb mod p = 19
knows doesn't know
p = 23 b = ?
base g = 5
a = 6
A = 5a mod 23
A = 56 mod 23 = 8
B = 19
s = Ba mod 23
s = 196 mod 23 = 2
s = 2
knows doesn't know
p = 23 a = ?
base g = 5
b = 15
B = 5b mod 23
B = 515 mod 23 = 19
A = 8
s = Ab mod 23
s = 815 mod 23 = 2
s = 2
knows doesn't know
p = 23 a = ?
base g = 5 b = ?
s = ?
A = 8
B = 19
s = 19a mod 23 = 8b mod 23
  • Now s = the shared secret key and it is known to both Alice and Bob, but not to Eve. s = 2

Note: It should be difficult for Alice to solve for Bob's private key or for Bob to solve for Alice's private key. If it is not difficult for Alice to solve for Bob's private key (or vice versa), Eve may simply substitute her own private / public key pair, plug Bob's public key into her private key, produce a fake shared secret key, and solve for Bob's private key (and use that to solve for the shared secret key. Eve may attempt to choose a public / private key pair that will make it easy for her to solve for Bob's private key). Another demonstration of Diffie-Hellman (also using numbers too small for practical use) is given here [7]

Operation with more than two parties

Diffie–Hellman key agreement is not limited to negotiating a key shared by only two participants. Any number of users can take part in an agreement by performing iterations of the agreement protocol and exchanging intermediate data (which does not itself need to be kept secret). For example, Alice, Bob, and Carol could participate in a Diffie–Hellman agreement as follows, with all operations taken to be modulo p:

  1. The parties agree on the algorithm parameters p and g.
  2. The parties generate their private keys, named a, b, and c.
  3. Alice computes g^a and sends it to Bob.
  4. Bob computes (g^a)^b = g^{ab} and sends it to Carol.
  5. Carol computes (g^{ab})^c = g^{abc} and uses it as her secret.
  6. Bob computes g^b and sends it to Carol.
  7. Carol computes (g^b)^c = g^{bc} and sends it to Alice.
  8. Alice computes (g^{bc})^a = g^{bca} = g^{abc} and uses it as her secret.
  9. Carol computes g^c and sends it to Alice.
  10. Alice computes (g^c)^a = g^{ca} and sends it to Bob.
  11. Bob computes (g^{ca})^b = g^{cab} = g^{abc} and uses it as his secret.

An eavesdropper has been able to see g^a, g^b, g^c, g^{ab}, g^{ac}, and g^{bc}, but cannot use any combination of these to reproduce g^{abc}.

To extend this mechanism to larger groups, two basic principles must be followed:

  • Starting with an “empty” key consisting only of g, the secret is made by raising the current value to every participant’s private exponent once, in any order (the first such exponentiation yields the participant’s own public key).
  • Any intermediate value (having up to N-1 exponents applied, where N is the number of participants in the group) may be revealed publicly, but the final value (having had all N exponents applied) constitutes the shared secret and hence must never be revealed publicly. Thus, each user must obtain their copy of the secret by applying their own private key last (otherwise there would be no way for the last contributor to communicate the final key to its recipient, as that last contributor would have turned the key into the very secret the group wished to protect).

These principles leave open various options for choosing in which order participants contribute to keys. The simplest and most obvious solution is to arrange the N participants in a circle and have N keys rotate around the circle, until eventually every key has been contributed to by all N participants (ending with its owner) and each participant has contributed to N keys (ending with their own). However, this requires that every participant perform N modular exponentiations.

By choosing a more optimal order, and relying on the fact that keys can be duplicated, it is possible to reduce the number of modular exponentiations performed by each participant to \log_2 (N) + 1 using a divide-and-conquer-style approach, given here for eight participants:

  1. Participants A, B, C, and D each perform one exponentiation, yielding g^{abcd}; this value is sent to E, F, G, and H. In return, participants A, B, C, and D receive g^{efgh}.
  2. Participants A and B each perform one exponentiation, yielding g^{efghab}, which they send to C and D, while C and D do the same, yielding g^{efghcd}, which they send to A and B.
  3. Participant A performs an exponentiation, yielding g^{efghcda}, which it sends to B; similarly, B sends g^{efghcdb} to A. C and D do similarly.
  4. Participant A performs one final exponentiation, yielding the secret g^{efghcdba} = g^{abcdefgh}, while B does the same to get g^{efghcdab} = g^{abcdefgh}; again, C and D do similarly.
  5. Participants E through H simultaneously perform the same operations using g^{abcd} as their starting point.

Once this operation has been completed all participants will possess the secret g^{abcdefgh}, but each participant will have performed only four modular exponentiations, rather than the eight implied by a simple circular arrangement.


The protocol is considered secure against eavesdroppers if G and g are chosen properly. The eavesdropper ("Eve") would have to solve the Diffie–Hellman problem to obtain gab. This is currently considered difficult. An efficient algorithm to solve the discrete logarithm problem would make it easy to compute a or b and solve the Diffie–Hellman problem, making this and many other public key cryptosystems insecure. Fields of small characteristic may be less secure.[8]

The order of G should have a large prime factor to prevent use of the Pohlig–Hellman algorithm to obtain a or b. For this reason, a Sophie Germain prime q is sometimes used to calculate p = 2q + 1, called a safe prime, since the order of G is then only divisible by 2 and q. g is then sometimes chosen to generate the order q subgroup of G, rather than G, so that the Legendre symbol of ga never reveals the low order bit of a.

If Alice and Bob use random number generators whose outputs are not completely random and can be predicted to some extent, then Eve's task is much easier.

The secret integers a and b are discarded at the end of the session. Therefore, Diffie–Hellman key exchange by itself trivially achieves perfect forward secrecy because no long-term private keying material exists to be disclosed.

In the original description, the Diffie–Hellman exchange by itself does not provide authentication of the communicating parties and is thus vulnerable to a man-in-the-middle attack. Mallory may establish two distinct key exchanges, one with Alice and the other with Bob, effectively masquerading as Alice to Bob, and vice versa, allowing her to decrypt, then re-encrypt, the messages passed between them. Note that Mallory must continue to be in the middle, transferring messages every time Alice and Bob communicate. If she is ever absent, her previous presence is then revealed to Alice and Bob. They will know that all of their private conversations had been intercepted and decoded by someone in the channel.

A method to authenticate the communicating parties to each other is generally needed to prevent this type of attack. Variants of Diffie–Hellman, such as STS protocol, may be used instead to avoid these types of attacks.

Other uses

Password-authenticated key agreement

When Alice and Bob share a password, they may use a password-authenticated key agreement (PAKE) form of Diffie–Hellman to prevent man-in-the-middle attacks. One simple scheme is to compare the hash of s concatenated with the password calculated independently on both ends of channel. A feature of these schemes is that an attacker can only test one specific password on each iteration with the other party, and so the system provides good security with relatively weak passwords. This approach is described in ITU-T Recommendation X.1035, which is used by the home networking standard.

Public key

It is also possible to use Diffie–Hellman as part of a public key infrastructure. Alice's public key is simply (g^a \bmod{p}, g, p). To send her a message, Bob chooses a random b and then sends Alice g^b \bmod p (un-encrypted) together with the message encrypted with symmetric key (g^a)^b \bmod{p}. Only Alice can decrypt the message because only she has a (the private key). A preshared public key also prevents man-in-the-middle attacks.

In practice, Diffie–Hellman is not used in this way, with RSA being the dominant public key algorithm. This is largely for historical and commercial reasons, namely that RSA Security created a certificate authority for key signing that became Verisign. Diffie–Hellman cannot be used to sign certificates. However, the ElGamal and DSA signature algorithms are mathematically related to it, as well as MQV, STS and the IKE component of the IPsec protocol suite for securing Internet Protocol communications.


The sender can produce only the public part of the key, whereas only the receiver can compute the private part. Because of that, the receiver is the only one who can release the funds after the transaction is committed. They need to perform a single-formula check on each transactions to establish if it belongs to them. This process involves their private key, therefore no third party can perform this check and discover the link between the one-time key generated by the sender and the receiver's unique public address.

See also


  1. ^ Synonyms of Diffie–Hellman key exchange include:
    • Diffie-Hellman-Merkle key exchange
    • Diffie–Hellman key agreement
    • Diffie–Hellman key establishment
    • Diffie–Hellman key negotiation
    • Exponential key exchange
    • Diffie–Hellman protocol
    • Diffie–Hellman handshake


  1. ^ Merkle, Ralph C (April 1978). "Secure Communications Over Insecure Channels". Communications of the ACM 21 (4): 294–299.  
  2. ^ a b  
  3. ^ Ellis, J. H. (January 1970). "The possibility of Non-Secret digital encryption". 
  4. ^ "GCHQ trio recognised for key to secure shopping online". BBC. 5 October 2010. Retrieved 5 August 2014. 
  5. ^ IEEE Communications Magazine Homepage | IEEE Communications Society. Retrieved on 2013-10-29.
  6. ^ US 4200770, Martin E. Hellman, Bailey W. Diffie, and Ralph C. Merkle, "Cryptographic apparatus and method", issued 29 April 1980 
  7. ^
  8. ^ A Heuristic Quasi-Polynomial Algorithm for Discrete Logarithm in Finite Fields of Small Characteristic," Razvan Barbulescu, Pierrick Gaudry, Antoine Joux, Emmanuel Thomé, Advances in Cryptology – EUROCRYPT 2014, Lecture Notes in Computer Science, Volume 8441, 2014, pp 1-16.

External links

  • Oral history interview with Martin Hellman, Charles Babbage Institute, University of Minnesota. Leading cryptography scholar Martin Hellman discusses the circumstances and fundamental insights of his invention of public key cryptography with collaborators Whitfield Diffie and Ralph Merkle at Stanford University in the mid-1970s.
  • RFC 2631 – Diffie–Hellman Key Agreement Method E. Rescorla June 1999.
  • Summary of ANSI X9.42: Agreement of Symmetric Keys Using Discrete Logarithm Cryptography (64K PDF file) (Description of ANSI 9 Standards)
  • Diffie–Hellman Key Exchange – A Non-Mathematician’s Explanation by Keith Palmgren
  • Crypt::DH Perl module from CPAN
  • Hands-on Diffie–Hellman demonstration
  • C implementation using GNU Multiple Precision Arithmetic Library
  • Diffie Hellman in 2 lines of Perl (using dc)
  • Smart Account Management (SAcct) (using DH key exchange to derive session key)
  • Diffie-Hellman Key Exchange - A YouTube video by Khan Academy faculty member Brit Cruise
  • Talk by Martin Hellman in 2007, Google video (broken link)
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.