Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Different private keys during the Diffie-Hellman exchange #3372

g1ts opened this Issue Jun 5, 2012 · 2 comments


None yet
3 participants

g1ts commented Jun 5, 2012

Sometimes when using Diffie-Hellman exchange generates different keys. Usually differ the last few byte(s).
You may see not correct behavior if you run this:

var crypto = require("crypto");

var server = crypto.createDiffieHellman(256);
var prime = server.getPrime();
var c = 0;
do {
    var alice = crypto.createDiffieHellman(prime);
    var alicePub = alice.getPublicKey();
    var bob = crypto.createDiffieHellman(prime);
    var bobPub = bob.getPublicKey();
    var bobAliceSecret = new Buffer(bob.computeSecret(alicePub)).toString('hex');
    var aliceBobSecret = new Buffer(alice.computeSecret(bobPub)).toString('hex');
} while (bobAliceSecret == aliceBobSecret)
console.log('c: '+c);
console.log('A: '+bobAliceSecret);
console.log('B: '+aliceBobSecret);

As far as I could understand the problem in node_crypto.cc in line 3942 (https://github.com/joyent/node/blob/master/src/node_crypto.cc#L3942)
When I calculated modPow() in Java for the same keys and prime in the result first byte(s) is zero. I think there's an error in the translation number to a string.
p.s. sorry for my English if it is not so clear


bnoordhuis commented Jun 5, 2012

Confirmed, thanks.

@ghost ghost assigned bnoordhuis and indutny Jun 5, 2012

@indutny indutny closed this in ae5b0e1 Jun 17, 2012


indutny commented Jun 17, 2012

Thanks, fixed in ae5b0e1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment