小编Dar*_*n J的帖子

使用 crypto 模块从 node.js 中的 Curve25519(或 X25519)非对称密钥对生成共享密钥

我正在尝试使用密钥交换算法(如Diffie Hellman密钥交换)在Curve25519 (或 X25519)非对称密钥对之间创建共享密钥。Diffie Hellman密钥交换可以在 node.js 中使用以下代码中的加密模块完成:

const crypto = require('crypto');

// Generate Alice's keys...
const alice = crypto.createDiffieHellman(2048);
const aliceKey = alice.generateKeys(); // Returns public key

// Generate Bob's keys...
const bob = crypto.createDiffieHellman(alice.getPrime(), alice.getGenerator());
const bobKey = bob.generateKeys(); // Returns public key

// Exchange and generate the secret...
const aliceSecret = alice.computeSecret(bobKey);
const bobSecret = bob.computeSecret(aliceKey);

// Should be equal
console.log(aliceSecret === bobSecret)
Run Code Online (Sandbox Code Playgroud)

X25519非对称密钥可以使用以下代码生成:

const crypto = require('crypto'); …
Run Code Online (Sandbox Code Playgroud)

encryption cryptography node.js diffie-hellman curve-25519

4
推荐指数
1
解决办法
4382
查看次数