网站搜索

javascript - 如何使用 AES-GCM 从 IE 11 加密操作的结果中解密数据


我已经设法在 Windows 10 上使用 IE 11 通过 AES-GCM 加密了一些数据,但我无法让解密工作。示例加密 JS 代码:

let plainText = new Uint8Array([1]);
let key;
let keyBuf = window.msCrypto.getRandomValues(new Uint8Array(32));
let iv = window.msCrypto.getRandomValues(new Uint8Array(12));
let additionalData = window.msCrypto.getRandomValues(new Uint8Array(16));
let encResult;
let importOp = window.msCrypto.subtle.importKey('raw', 
    keyBuf,
    { name: 'AES-GCM' }, 
    false, 
    ['encrypt', 'decrypt']);
importOp.oncomplete = function(e) {
    key = e.target.result;
    let encryptOp = window.msCrypto.subtle.encrypt({
        name: 'AES-GCM',
        iv: iv,
        tagLength: 128,
        additionalData: additionalData
    }, key, plainText);
    encryptOp.oncomplete = function (e) {
        encResult = e.target.result;
    };
};

生成的项目 (encResult) 是一个 AesGcmEncryptResult,它在两个不同的属性中包含加密值和标签。据我所知,我需要将它们连接起来,并将其作为密文传递以进行解密,如下所示:

let cipherText = new Uint8Array(plainText.length + 16); // tagLength / 8
cipherText.set(new Uint8Array(encResult.ciphertext), 0);
cipherText.set(new Uint8Array(encResult.tag), plainText.length);
let decryptOp = window.msCrypto.subtle.decrypt({
    name: 'AES-GCM',
    iv: iv,
    tagLength: 128,
    additionalData: additionalData
}, key, cipherText);

然后我连接 oncomplete 和 onerror,结果触发了 onerror。不幸的是,IE 的 Event 对象除了 type="error" 之外,没有其他信息可以告诉我。

网上关于在 IE 11 中使用 AES-GCM 的信息非常少。

请不要告诉我使用其他浏览器。这在 Chrome 和 Firefox 中一切正常(只是表现不同)。我特别想让它在 IE 11 中运行。

我漏掉了什么?

答案#1

我找到了这个垫片,它(模糊地)显示了标签值放在算法对象中,而密文单独放在第三个参数中。例如。

let decryptOp = window.msCrypto.subtle.decrypt({
    name: 'AES-GCM',
    iv: iv,
    additionalData: additionalData,
    tag: new Uint8Array(encResult.tag)
    }, key, new Uint8Array(encResult.ciphertext));

为什么这个东西这么难找到?为什么没有关于这个功能的博客文章?为什么微软的文档细节如此简略?