user820955
user820955

Reputation: 91

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.

Upvotes: 3

Views: 1942

Answers (1)

Woodstock
Woodstock

Reputation: 22956

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.

Upvotes: 4

Related Questions