当前位置:首页 > JavaScript

js实现ecdh

2026-03-15 02:29:15JavaScript

ECDH 密钥交换的 JavaScript 实现

ECDH(椭圆曲线 Diffie-Hellman)是一种基于椭圆曲线密码学的密钥交换协议。以下是使用 Web Crypto API 在 JavaScript 中实现 ECDH 密钥交换的方法。

生成密钥对

使用 generateKey 方法生成 ECDH 密钥对。需要指定椭圆曲线名称(如 P-256)和密钥用途。

js实现ecdh

const generateKeyPair = async () => {
  return await window.crypto.subtle.generateKey(
    {
      name: "ECDH",
      namedCurve: "P-256",
    },
    true,
    ["deriveKey", "deriveBits"]
  );
};

导出公钥

将公钥导出为可传输的格式(如 JWK 或 ArrayBuffer)。

js实现ecdh

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 模块的类似功能。

标签: jsecdh
分享给朋友:

相关文章

js实现

js实现

实现 JavaScript 功能的方法 在 JavaScript 中实现特定功能通常涉及多个步骤。以下是常见的实现方法: 基础语法和变量声明 使用 let 或 const 声明变量: let co…

js实现拖拽

js实现拖拽

实现拖拽的基本步骤 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽功能。以下是核心代码逻辑: const draggableElement = document.…

js实现复制功能

js实现复制功能

使用 document.execCommand 方法 这种方法适用于较旧的浏览器,但在现代浏览器中可能被逐步淘汰。通过创建一个临时的 textarea 元素,将文本内容放入其中,然后执行复制命令。…

js实现图片预览

js实现图片预览

使用FileReader API实现图片预览 通过FileReader对象读取用户选择的图片文件并显示预览: const input = document.getElementById('image…

js如何实现继承

js如何实现继承

原型链继承 通过将子类的原型指向父类的实例实现继承。子类实例能够访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Parent…

js实现拷贝

js实现拷贝

实现文本拷贝 使用 document.execCommand 方法(已废弃但兼容性较好): function copyText(text) { const textarea = document…