我正在尝试理解diffie hellman密钥交换是如何工作的,并编写了一个简单的node.js程序来进行测试:
var crypto = require("crypto");
//create a public & private key for alice
var Alice = crypto.createDiffieHellman(512);
Alice.generateKeys();
//create a public & private key for bob
var Bob = crypto.createDiffieHellman(512);
Bob.generateKeys();
//let bob and alice compute the shared secret
console.log(Alice.computeSecret(Bob.getPublicKey()).toString("hex"));
console.log(Bob.computeSecret(Alice.getPublicKey()).toString("hex"));
如果我理解this维基百科文章中的正确图形,那么这两个秘密应该是相同的。但他们永远不会,为什么?
答案 0 :(得分:0)
啊,我已经找到了解决方案:
var crypto = require("crypto");
var prime = crypto.createDiffieHellman(512).getPrime();
//create a public & private key for alice
var Alice = crypto.createDiffieHellman(prime);
Alice.generateKeys();
//create a public & private key for bob
var Bob = crypto.createDiffieHellman(prime);
Bob.generateKeys();
//let bob and alice compute the shared secret
console.log(Alice.computeSecret(Bob.getPublicKey()).toString("hex"));
console.log(Bob.computeSecret(Alice.getPublicKey()).toString("hex"));