javascript - 使用 webcrypto API 的 AES_GCM 加密
问题描述
我遇到了一个 AES_GCM 实现,它的加密功能如下:
async function encryptMessage(key){
let encodedMessage = encodeMessage();
cipherText = await window.crypto.subtle.encrypt(
{
name: "AES-GCM",
iv: window.crypto.getRandomValues(new Uint8Array(12))
},
key,
encodedMessage
);
let buffer = new Uint8Array(cipherText, 0, 5);
console.log(buffer);
console.log(btoa(buffer));
}
我的问题是,为什么
新的 Uint8Array(cipherText, 0, 5);
有参数0和5。我在网上搜索,发现这些参数代表一个视图。但为什么只有 0 和 5?我们可以使用这些以外的数字吗?
解决方案
我认为该示例来自https://github.com/mdn/dom-examples/blob/master/web-crypto/encrypt-decrypt/aes-gcm.js,这是 MDN 示例的一部分。
我的理解是他们只是向用户显示前几个字符,以证明它已被加密。您将看到真实内容保存在 中cipherText
,其中还有iv
一个模块级变量。
推荐阅读
- jsoup - 如何阻止 jsoup.clean 取消转义 € 和其他特殊字符
- python-3.x - 正则表达式使用 match 方法匹配 python3 中的模式
- c - 使用 Tcl C API 的 Lib 崩溃可能是由于错误的 refCount 使用
- prolog - Prolog:过滤目标列表并保留成功的目标
- javascript - 如何将 .content().filter 方法从 jquery 转换为 vanilla javascript
- swift - 最小化 macOS 应用程序 + 分布式通知中心(macOS 上的后台模式)
- c++ - qt中的弹出小部件
- python - 使用异步队列时如何修补类实例方法
- c++ - 帕斯卡三角失败超过第 14 行
- javascript - AdminBro 中文件上传的自定义提供程序问题