未捕获(承诺)DOMException:AES密钥数据必须为128或256位

时间:2018-09-03 10:45:15

标签: javascript encryption

使用以下代码,我收到有关AES密钥长度不正确的错误,但它是恕我直言:-(

async function Test() {
        var passphraseKey = await TextEncode("I hëart årt and £$¢!");
        var data = await TextEncode("I hëart årt and £$¢!");
        var saltBuffer = await window.crypto.getRandomValues(new Uint8Array(16));

        var importKey = await window.crypto.subtle.importKey('raw', passphraseKey, { name: 'PBKDF2' }, false, ['deriveKey'])
            .then(function (key) {
                return key;
            });

        var deriveKey = await window.crypto.subtle.deriveKey({ name: 'PBKDF2', salt: saltBuffer, iterations: 100, hash: 'SHA-256' }, importKey, { name: 'AES-GCM', length: 256 }, true, ["encrypt", "decrypt"])
            .then(function (webKey) {
                return webKey;
            });

        var cipher = await window.crypto.subtle.encrypt({ name: "AES-GCM", iv: window.crypto.getRandomValues(new Uint8Array(16)) }, deriveKey, data)
            .then(function (encrypted) {
                console.log(new Uint8Array(encrypted));
            })
            .catch(function (err) {
                console.error(err);
            });
    };

是window.crypto.subtle.encrypt引发错误,但是实际上我得到的是有效输出,但是为什么它同时引发错误?

带有错误消息的加密输出 enter image description here

0 个答案:

没有答案