概述SubtleCrypto 提供原生高安全的密码学操作。AES-GCM 用于高效对称加密,RSA-OAEP 用于非对称密钥封装。需在安全上下文中运行。用法/示例// AES-GCM 加解密

const key = await crypto.subtle.importKey('raw', crypto.getRandomValues(new Uint8Array(32)), { name: 'AES-GCM' }, false, ['encrypt','decrypt'])

const iv = crypto.getRandomValues(new Uint8Array(12))

const ciphertext = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, new TextEncoder().encode('secret'))

const plaintext = await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, ciphertext)

// RSA-OAEP 解封装

const spki = /* ArrayBuffer of public key */

const pub = await crypto.subtle.importKey('spki', spki, { name: 'RSA-OAEP', hash: 'SHA-256' }, false, ['encrypt'])

const oaep = await crypto.subtle.encrypt({ name: 'RSA-OAEP' }, pub, new Uint8Array([1,2,3]))

工程建议使用随机 `iv` 与唯一性保障,避免重放与重用;对密钥材料进行安全存储与轮换。选择合适的参数(如 `SHA-256`)并验证兼容矩阵;避免自定义不安全方案。遵守隐私与合规要求,对敏感数据进行最小化与访问审计。参考与验证MDN:Web Crypto — https://developer.mozilla.org/docs/Web/API/Web_Crypto_APIW3C:Web Crypto — https://www.w3.org/TR/WebCryptoAPI/

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部