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

标签 javascript encryption internet-explorer-11 aes-gcm webcrypto-api

我在 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,它具有加密值和 2 个不同属性中的标记。据我了解,我需要将它们连接起来并将它们作为密文传递给解密,如:

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 触发。不幸的是,除了 type = "error"之外,IE 的 Event 对象没有任何信息可以告诉我。

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

请不要告诉我使用不同的浏览器。这一切都适用于 Chrome 和 Firefox(但不同)。我特别想让它在 IE 11 中运行。

我错过了什么?

最佳答案

我找到了这个 shim这(模糊地)表明标签值进入算法对象,而密文单独进入第三个参数。例如

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

为什么这么难找?为什么没有关于此功能的博客文章?为什么 MS 的文档在细节上如此简短?

关于javascript - 如何使用 AES-GCM 从 IE 11 加密操作的结果中解密数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41449185/

相关文章:

javascript - 为什么readline会卡住?

java - 使用 Java 独立应用程序的 SSL 连接

encryption - substratTEE与Intel SGX威胁模型的差异对比

javascript - 检查 "-ms-expand"支持

(未知源位置)第 5 行第 9 列的 JavaScript 严重错误

javascript - 在 HTML5 Canvas Javascript 中隐藏鼠标光标

javascript - 带有来自 url(json 数据)的商店的 dojo dgrid 重新加载/刷新按钮

javascript - Moment JS 在某些 PC 上显示出 1 小时的差异

java - android N 中解密失败

javascript - 通过 Selenium 和 Java 使用 IEDriverServer 和 Internet Explorer 的 Microsoft Dynamics CRM 中的“XPathEvaluator 未定义”脚本错误