AesGcmParams
AesGcmParams
对象表示在使用 AES-GCM 算法时应作为算法参数传递给 encrypt 和 decrypt 操作的对象。
更多详情,请参阅 MDN Web Crypto API 关于 AES-GCM 的文档。
属性
属性 | 类型 | 描述 |
---|---|---|
name | string | 应设置为 AES-GCM 。 |
iv | ArrayBuffer 、TypedArray 或 DataView | 初始化向量。它必须是 12 字节长,不可预测且具有密码学随机性。对于使用给定密钥执行的每次加密操作,它必须是唯一的。切勿对同一密钥重复使用 iv。然而,它不必是秘密的,可以与密文一起传输。 |
additionalData (可选) | ArrayBuffer 、TypedArray 或 DataView | 应进行认证但不加密的附加数据。它必须包含在认证标签的计算中,但本身不进行加密。 |
tagLength (可选) | number | 认证标签的长度(比特)。应设置,默认值为 96。 |
示例
export default async function () {
const plaintext = stringToArrayBuffer('Hello, World!');
/**
* Generate a symmetric key using the AES-CBC algorithm.
*/
const key = await crypto.subtle.generateKey(
{
name: 'AES-GCM',
length: 256,
},
true,
['encrypt', 'decrypt']
);
/**
* Encrypt the plaintext using the AES-CBC key with
* have generated.
*/
const ciphertext = await crypto.subtle.encrypt(
{
name: 'AES-GCM',
iv: crypto.getRandomValues(new Uint8Array(12)),
},
key,
plaintext
);
}
function stringToArrayBuffer(str) {
const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
const bufView = new Uint16Array(buf);
for (let i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}