async function aesSimple() {
const key = await crypto.subtle.generateKey(
{name: "AES-GCM", length: 256}, true, ["encrypt", "decrypt"]
);
const iv = crypto.getRandomValues(new Uint8Array(12));
const encrypted = await crypto.subtle.encrypt(
{name: "AES-GCM", iv}, key, new TextEncoder().encode("你好世界")
);
const decrypted = await crypto.subtle.decrypt(
{name: "AES-GCM", iv}, key, encrypted
);
console.log("结果:", new TextDecoder().decode(decrypted));
}
aesSimple();
class ECCFlow {
static async demonstrateCompleteFlow() {
console.log("ECC 密钥交换完整流程演示");
console.log("==========================");
console.log("\n第 1 步:选择椭圆曲线");
const curve = "P-256";
console.log(" 选择的曲线:" + curve);
console.log(" 安全强度:128 位");
console.log(" 公钥长度:65 字节");
console.log(" 私钥长度:32 字节");
console.log("\n第 2 步:双方生成密钥对");
const aliceKeyPair = await crypto.subtle.generateKey(
{ name: "ECDH", namedCurve: curve }, true, ["deriveKey"]
);
console.log(" Alice 生成密钥对:");
console.log(" - 私钥:保密存储");
console.log(" - 公钥:准备发送给 Bob");
const bobKeyPair = await crypto.subtle.generateKey(
{ name: "ECDH", namedCurve: curve }, true, ["deriveKey"]
);
console.log(" Bob 生成密钥对:");
console.log(" - 私钥:保密存储");
console.log(" - 公钥:准备发送给 Alice");
console.log("\n第 3 步:交换公钥(通过网络)");
const bobPublicKey = bobKeyPair.publicKey;
console.log(" Alice 收到 Bob 的公钥");
const alicePublicKey = aliceKeyPair.publicKey;
console.log(" Bob 收到 Alice 的公钥");
console.log("\n第 4 步:各自计算共享密钥");
console.log(" 数学原理:");
console.log(" Alice 私钥 × Bob 公钥 = Bob 私钥 × Alice 公钥");
const aliceSharedKey = await crypto.subtle.deriveKey(
{ name: "ECDH", public: bobPublicKey }, aliceKeyPair.privateKey,
{ name: "AES-GCM", length: 256 }, true, ["encrypt", "decrypt"]
);
console.log(" Alice 计算共享密钥完成");
const bobSharedKey = await crypto.subtle.deriveKey(
{ name: "ECDH", public: alicePublicKey }, bobKeyPair.privateKey,
{ name: "AES-GCM", length: 256 }, true, ["encrypt", "decrypt"]
);
console.log(" Bob 计算共享密钥完成");
console.log("\n第 5 步:验证双方密钥相同");
const aliceKeyBytes = await crypto.subtle.exportKey("raw", aliceSharedKey);
const bobKeyBytes = await crypto.subtle.exportKey("raw", bobSharedKey);
const aliceHex = Array.from(new Uint8Array(aliceKeyBytes))
.map(b => b.toString(16).padStart(2, '0'))
.join('');
const bobHex = Array.from(new Uint8Array(bobKeyBytes))
.map(b => b.toString(16).padStart(2, '0'))
.join('');
console.log(" 验证结果:");
console.log(" Alice 密钥:" + aliceHex.substring(0, 32) + "...");
console.log(" Bob 密钥:" + bobHex.substring(0, 32) + "...");
console.log(" 是否相同:" + (aliceHex === bobHex ? "是" : "否"));
console.log("\n第 6 步:使用共享密钥加密通信");
const message = "会议时间改为下午 2 点";
console.log(" Alice 要发送:\"" + message + "\"");
const iv = crypto.getRandomValues(new Uint8Array(12));
const encrypted = await crypto.subtle.encrypt(
{ name: "AES-GCM", iv }, aliceSharedKey, new TextEncoder().encode(message)
);
console.log(" 加密完成:");
console.log(" - 密文长度:" + encrypted.byteLength + " 字节");
console.log(" - IV: " + Array.from(iv).slice(0, 3).join(',') + "...");
console.log("\n第 7 步:Bob 解密消息");
const decrypted = await crypto.subtle.decrypt(
{ name: "AES-GCM", iv }, bobSharedKey, encrypted
);
const decryptedMessage = new TextDecoder().decode(decrypted);
console.log(" Bob 解密得到:\"" + decryptedMessage + "\"");
console.log(" 是否正确:" + (message === decryptedMessage ? "是" : "否"));
console.log("\n安全性分析:");
console.log(" 中间人无法计算共享密钥");
console.log(" 即使窃听到公钥交换也没用");
console.log(" 每次会话可生成新的密钥对");
return { aliceKeyPair, bobKeyPair, sharedKey: aliceSharedKey, encryptedMessage: encrypted };
}
}
ECCFlow.demonstrateCompleteFlow().catch(console.error);
async function generateRSAKeys() {
const keyPair = await crypto.subtle.generateKey(
{ name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: "SHA-256"
}, true,
["encrypt", "decrypt"]
);
return { publicKey: keyPair.publicKey, privateKey: keyPair.privateKey };
}
async function rsaEncryptDecrypt() {
console.log("=== RSA 加密解密示例 ===");
const { publicKey, privateKey } = await generateRSAKeys();
console.log("密钥对生成完成");
const message = "这是一条秘密消息";
const data = new TextEncoder().encode(message);
const encrypted = await crypto.subtle.encrypt(
{ name: "RSA-OAEP" }, publicKey, data
);
console.log("加密完成,密文长度:", encrypted.byteLength, "字节");
const decrypted = await crypto.subtle.decrypt(
{ name: "RSA-OAEP" }, privateKey, encrypted
);
const decryptedMessage = new TextDecoder().decode(decrypted);
console.log("解密结果:", decryptedMessage);
return { publicKey, privateKey, encrypted };
}
rsaEncryptDecrypt().catch(console.error);