Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Diffie-Hellman with NodeJS

// node.js 0.5 Diffie-Hellman example
var crypto = require("crypto");

// the prime is shared by everyone                                              
var server = crypto.createDiffieHellman(512);
var prime = server.getPrime();

// sharing secret key on a pair                                                 
var alice = crypto.createDiffieHellman(prime);
alice.generateKeys();
var alicePub = alice.getPublicKey();

var bob = crypto.createDiffieHellman(prime);
bob.generateKeys();
var bobPub = bob.getPublicKey();

var bobAliceSecret = bob.computeSecret(alicePub);
var aliceBobSecret = alice.computeSecret(bobPub); 

I am trying to understand how to use the NodeJS crypto library for a diffie-hellman implementation, and got the above code to compute a shared secret. The problem is both Alice and Bob generate their keys after getting the shared prime. I need them to generate their respective key pairs without having to use any shared information, later than can use shared information to compute the shared secret. I can't get to see how that can be done using the NodeJS crypto library.

like image 826
user820955 Avatar asked Feb 06 '26 20:02

user820955


1 Answers

I see your confusion.

The Diffie-Hellman prime represents some group of numbers (cyclic group) that you perform the DH function inside, however it's not randomly generated for each person.

Check out my answer here.

The prime/group is known prior to key generation and static.

There are only few groups used, see here for more.

To be clear, in order to generate the same keys, you just need to make sure both ppl are operating inside the same group with the same DH params.

like image 65
Woodstock Avatar answered Feb 09 '26 12:02

Woodstock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!