# How is a public/private key pair generated from a Diffe-Hellman key exchange?

I very well understand the DH key exchange. But what is the public key and private key at the end of the exchange.

For example: Alice and Bob agree to use a modulus $p = 23$ and base $g = 5$ (which is a primitive root modulo $23$).

Alice chooses a secret integer $a = 6$, then sends Bob $A = g^a \bmod p = 5^6 \bmod 23 = 8$

Bob chooses a secret integer $b = 15$, then sends Alice $B = g^b \bmod p = 5^{15} \bmod 23 = 19$

Alice computes $s = B^a \bmod p = 19^6 \bmod 23 = 2$

Bob computes $s = A^b \bmod p = 8^{15} \bmod 23 = 2$

Alice and Bob now share a secret (the number $2$).

So is $2$ the private key here ? If it's a private key then both Alice and Bob know it even though the eavesdroppers don't know. Please explain how public key/private key pair is generated from this shared secret $2$.

• @otus so u mean DH key exchange is a symmetric key exchange protocol. So after the shared secret 2 is derived both Alice and bob start encrypting. Is it? But then why does diffe hellman often come under the context of public key/asymmetric cryptography. How does it help in asymmetry? – callyaser Sep 24 '15 at 13:21
• @gilles for instance, say Bob is the server that Alice wants to communicate with. So in every cryptography class its told that Bob shares its public key to the clients interested to communicate with it (Alice is the client in our case). So the public key it sends Alice is B=19. Alice encrypts her message using this which can be decrypted using Bobs private key b=15.is it how it works? – callyaser Sep 24 '15 at 13:30
• No, Alice does not encrypt her message with Bob's public key. That's what is done for encryption algorithms such as RSA. But Diffie-Hellman is a key exchange algorithm, not an encryption algorithm. DH isn't for doing asymmetric encryption or signature, it generates a shared secret. The shared secret can be used for encryption, but then it's symmetric encryption (e.g. with AES). – Gilles 'SO- stop being evil' Sep 24 '15 at 15:08
• @gilles So at the end of diffe hellman key exchange, AES symmetric encryption is run with the generated shared secret 2 (in this case its a 1 bit key. But in reality the generated shared key is 1024 bits long. ). I hope I am right now. But then my question was how would a public/private key pair be generated. Does diffe hellman exchange aid in that generation? Because, in every cryptography class what's being said is that if a client wants to communicate with the server a public key is sent by the server (bob in my assumption) to the client and it's decrypted by the server's private key. – callyaser Sep 24 '15 at 18:58
• There are plenty of different protocols. The protocol you describe uses a key pair to allow one party to encrypt data which only the other party can decrypt. DH isn't useful for that. It's useful for obtaining a shared secret key. I really can't explain all possible uses of cryptography in a comment or even in an answer. Rest assured that you will discover more protocols as the course progresses. – Gilles 'SO- stop being evil' Sep 24 '15 at 21:48

So is 2 the private key here ?

No, it's referred to as a "shared secret" (because it is shared between Alice and Bob, and is secret to everyone else).

If there were 'private' and 'public' keys (which is not the standard terminology with DH), then Alice's private key would be $a=6$, and the public key would be $g^a = 8$. In this case, the 'private key' is private, in the sense that it allows her to perform the operation of deriving the shared secret; something that we hope is difficult if someone has only the public keys.

$s$ is a shared secret key. It's known to both Alice and Bob. You could call is a private key, but the usual terminology is “secret key” here, for no deep reason.

Alice has a private/public key pair: $a$ is her private key, $A$ is her public key. Ditto with $b$ and $B$ for Bob. These values are not useful in isolation though; in normal use, the only point in running a Diffie-Hellman key exchange is to generate $s$, after that $a$ can be discarded.

So is $2$ the private key here ? If it's a private key then both Alice and Bob know it even though the eavesdroppers don't know. Please explain how public key/private key pair is generated from this shared secret $2$.

As the others said, $2$ is a shared secret, rather than a private key.

It is usually used to derive one or more symmetric keys (e.g. for AES and some MAC). It could be used to derive a key pair, but since both Alice and Bob would know it there would little advantage in doing so. Better to use more efficient symmetric algorithms from there on, which is usually the whole reason for doing Diffie–Hellman.