js实现rsa
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 格式:

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):

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.subtleAPI 仅在 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);






