js实现ecdh
ECDH 密钥交换的 JavaScript 实现
ECDH(椭圆曲线 Diffie-Hellman)是一种基于椭圆曲线密码学的密钥交换协议。以下是使用 Web Crypto API 在 JavaScript 中实现 ECDH 密钥交换的方法。
生成密钥对
使用 generateKey 方法生成 ECDH 密钥对。需要指定椭圆曲线名称(如 P-256)和密钥用途。

const generateKeyPair = async () => {
return await window.crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-256",
},
true,
["deriveKey", "deriveBits"]
);
};
导出公钥
将公钥导出为可传输的格式(如 JWK 或 ArrayBuffer)。

const exportPublicKey = async (publicKey) => {
return await window.crypto.subtle.exportKey(
"jwk",
publicKey
);
};
派生共享密钥
使用本地私钥和对方公钥派生共享密钥。
const deriveSharedSecret = async (privateKey, publicKey) => {
return await window.crypto.subtle.deriveKey(
{
name: "ECDH",
public: publicKey,
},
privateKey,
{
name: "AES-GCM",
length: 256,
},
true,
["encrypt", "decrypt"]
);
};
完整示例流程
以下是完整的 ECDH 密钥交换示例:
(async () => {
// 双方生成密钥对
const aliceKeyPair = await generateKeyPair();
const bobKeyPair = await generateKeyPair();
// 交换公钥
const alicePublicKey = await exportPublicKey(aliceKeyPair.publicKey);
const bobPublicKey = await exportPublicKey(bobKeyPair.publicKey);
// 导入对方公钥
const importedAlicePublicKey = await window.crypto.subtle.importKey(
"jwk",
alicePublicKey,
{
name: "ECDH",
namedCurve: "P-256",
},
true,
[]
);
const importedBobPublicKey = await window.crypto.subtle.importKey(
"jwk",
bobPublicKey,
{
name: "ECDH",
namedCurve: "P-256",
},
true,
[]
);
// 派生共享密钥
const aliceSharedSecret = await deriveSharedSecret(
aliceKeyPair.privateKey,
importedBobPublicKey
);
const bobSharedSecret = await deriveSharedSecret(
bobKeyPair.privateKey,
importedAlicePublicKey
);
// 验证双方密钥是否相同
const aliceExported = await window.crypto.subtle.exportKey("raw", aliceSharedSecret);
const bobExported = await window.crypto.subtle.exportKey("raw", bobSharedSecret);
console.log(
"Shared secrets match:",
new Uint8Array(aliceExported).toString() ===
new Uint8Array(bobExported).toString()
);
})();
注意事项
- 使用安全的椭圆曲线(如 P-256、P-384 或 P-521)
- 确保正确处理密钥的导入导出格式
- 共享密钥可用于后续加密通信(如 AES 加密)
- 浏览器兼容性:Web Crypto API 在现代浏览器中得到广泛支持
这种方法提供了原生、高效的 ECDH 实现,无需依赖第三方库。对于 Node.js 环境,可以使用 crypto 模块的类似功能。






