当前位置:首页 > JavaScript

js实现rsa

2026-04-06 05:38:29JavaScript

RSA 加密算法简介

RSA 是一种非对称加密算法,广泛应用于数据加密和数字签名。它基于大数分解的数学难题,安全性依赖于大整数分解的困难性。RSA 包含密钥生成、加密和解密三个主要步骤。

生成 RSA 密钥对

在 JavaScript 中可以使用 crypto.subtle API 生成 RSA 密钥对。以下是一个生成 RSA-OAEP 密钥对的示例:

async function generateRSAKeyPair() {
  const keyPair = await window.crypto.subtle.generateKey(
    {
      name: "RSA-OAEP",
      modulusLength: 2048,
      publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
      hash: "SHA-256",
    },
    true,
    ["encrypt", "decrypt"]
  );
  return keyPair;
}

加密数据

使用公钥加密数据时,需要将数据转换为 ArrayBuffer 格式:

js实现rsa

async function encryptData(publicKey, data) {
  const encodedData = new TextEncoder().encode(data);
  const encryptedData = await window.crypto.subtle.encrypt(
    {
      name: "RSA-OAEP",
    },
    publicKey,
    encodedData
  );
  return encryptedData;
}

解密数据

使用私钥解密数据时,同样需要处理 ArrayBuffer 格式:

async function decryptData(privateKey, encryptedData) {
  const decryptedData = await window.crypto.subtle.decrypt(
    {
      name: "RSA-OAEP",
    },
    privateKey,
    encryptedData
  );
  return new TextDecoder().decode(decryptedData);
}

导出和导入密钥

密钥可以导出为可传输或存储的格式(如 JWK 或 PEM):

js实现rsa

async function exportKey(key, format = 'jwk') {
  const exported = await window.crypto.subtle.exportKey(format, key);
  return exported;
}

async function importPublicKey(jwkKey) {
  const publicKey = await window.crypto.subtle.importKey(
    'jwk',
    jwkKey,
    {
      name: "RSA-OAEP",
      hash: "SHA-256",
    },
    true,
    ["encrypt"]
  );
  return publicKey;
}

注意事项

  • RSA-OAEP 是推荐的填充方案,比 PKCS#1 v1.5 更安全。
  • 密钥长度至少应为 2048 位,更高的安全性需要 3072 或 4096 位。
  • 浏览器环境中的 crypto.subtle API 仅在 HTTPS 或 localhost 下可用。

完整示例

以下是一个完整的 RSA 加密解密示例:

async function runExample() {
  // 生成密钥对
  const keyPair = await generateRSAKeyPair();

  // 导出公钥
  const publicKeyJwk = await exportKey(keyPair.publicKey);

  // 模拟传输公钥
  const importedPublicKey = await importPublicKey(publicKeyJwk);

  // 加密数据
  const originalData = "Hello, RSA!";
  const encryptedData = await encryptData(importedPublicKey, originalData);

  // 解密数据
  const decryptedData = await decryptData(keyPair.privateKey, encryptedData);
  console.log("Decrypted data:", decryptedData);
}

runExample();

Node.js 环境实现

在 Node.js 中可以使用 crypto 模块实现 RSA:

const crypto = require('crypto');

// 生成密钥对
const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
  modulusLength: 2048,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem'
  }
});

// 加密
function encrypt(publicKey, data) {
  return crypto.publicEncrypt(publicKey, Buffer.from(data));
}

// 解密
function decrypt(privateKey, encryptedData) {
  return crypto.privateDecrypt(privateKey, encryptedData).toString();
}

// 示例
const encrypted = encrypt(publicKey, 'Secret message');
const decrypted = decrypt(privateKey, encrypted);
console.log(decrypted);

标签: jsrsa
分享给朋友:

相关文章

js实现

js实现

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

js实现拖拽

js实现拖拽

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

js实现乘

js实现乘

JavaScript 实现乘法运算 在 JavaScript 中实现乘法运算可以通过多种方式完成,包括基本运算符、函数封装以及高级算法(如大数乘法)。以下是几种常见方法: 使用基本乘法运算符 Jav…

js实现tab选项卡切换

js实现tab选项卡切换

实现Tab选项卡切换的JavaScript方法 使用纯JavaScript实现Tab切换功能,可以通过监听点击事件动态切换内容显示状态。以下是两种常见实现方式: 基础DOM操作实现 /…

js钟表实现

js钟表实现

实现JavaScript钟表的基本方法 创建一个简单的JavaScript钟表可以通过以下步骤完成,涵盖数字和模拟两种形式。 数字钟表实现 HTML结构只需一个显示时间的容器: <div i…

js 实现日历

js 实现日历

实现日历的基本思路 日历的核心功能是展示日期,并允许用户进行日期选择或导航。JavaScript 可以动态生成日历的 HTML 结构,并处理用户交互逻辑。 基础日历结构 日历通常包含头部(显示月份和…