I've just made my own implementation of Diffie-Hellman in nodejs (it's for a project of mine). However, the shared keys are integers; I want to use those integers as a key to encrypt data. This is an example output:

Publicly Shared Variables:
    Publicly Shared Prime:      9811
    Publicly Shared Base:        844

Private Variables:
    Alice Secret:                  6
    Bob Secret:                   15

Alice Sends Over Public Chanel:  905

Bob Sends Over Public Chanel:   1168

Privately Calculated Shared Secret:
    Alice Shared Secret:        3793
    Bob Shared Secret:          3793

What would be the best encryption method, using the key of those integers? Sorry, this is a bit hard to explain.


1 Answer 1


What would be the best encryption method[...]?

The standard approach is quite simple. You have a Diffie-Hellman shared secret $K$. Because $K$ is not actually uniformly distributed you run $K$ through a key-based key derivation function (KBKDF) like HKDF (or even a "simple" hash), this is your key $k$ then. Then you use $k$ to symmetrically encrypt (and authenticate) data using a unique nonce for each data packet. You can also derive multiple different keys from the shared secret, e.g. one for each transmission direction.

You may want to note though that your current parameters are a bit on the insecure side being only 12-bit long as opposed to the current standard 2048-bit length.

  • 1
    $\begingroup$ You would need to encode the shared secret before using it as input for the KDF, preferably using I2OSP: an unsigned, big endian value with the same size as the key size. This is the default (and which is now integrated into TLS 1.3, as I proposed it for changeset 13). $\endgroup$
    – Maarten Bodewes
    Commented Aug 12, 2018 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.