我试图了解 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"));
如果我正确理解了这篇维基百科文章中的正确图形,那么这两个秘密应该是相同的。但他们从来都不是,为什么?