问题描述
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,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
一起是一个模块级变量。