⚡ 编程实验室🏗️ HTML🎨 CSS⚡ JavaScript🐍 Python🗄️ SQL☕ Java⚛️ React💚 Vue🟢 Node.js⚙️ C语言🐘 PHP🐹 Go🔷 TypeScript🐬 MySQL🔧 C++🎯 C#🦀 Rust🅱️ Bootstrap💡 jQuery🎸 Django🍃 MongoDB👗 Sass🎪 Kotlin📊 R语言📋 XML📊 Excel🐘 PostgreSQL🐳 Docker🅰️ Angular🎮 游戏🏠 网站首页

Web Crypto API:在浏览器中实现端到端加密聊天

使用SubtleCrypto接口生成RSA密钥对,通过AES-GCM加密消息,实现安全通信。 · 难度:入门 · +10XP

Web Crypto API:在浏览器中实现端到端加密聊天

Web Crypto API提供原生的加密原语,避免依赖第三方库。本教程演示:1) 使用crypto.subtle.generateKey生成RSA-OAEP密钥对;2) 用AES-GCM加密实际消息,并用RSA公钥加密AES密钥(混合加密);3) 导出密钥为JWK格式便于传输。重点讲解IV(初始化向量)的随机生成、密钥派生以及如何安全存储私钥。

async function encryptMessage(plaintext, publicKey) {
  const aesKey = await crypto.subtle.generateKey(
    { name: 'AES-GCM', length: 256 }, true, ['encrypt']
  );
  const iv = crypto.getRandomValues(new Uint8Array(12));
  const encrypted = await crypto.subtle.encrypt(
    { name: 'AES-GCM', iv }, aesKey, plaintext
  );
  const wrappedKey = await crypto.subtle.wrapKey(
    'raw', aesKey, publicKey, { name:'RSA-OAEP' }
  );
  return { encrypted, iv, wrappedKey };
}
Ctrl+Enter
🚀 升级VIP
解锁全部课程+AI助手

🏆 学习排行

加载中...

📊 统计

📖 231 篇
0 完成
🔥 0