0

Using the below code, I get an error regarding AES key is not of correct length, but it is IMHO :-(

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);
            });
    };

It is window.crypto.subtle.encrypt that throws the error, but actually I end up with a valid output, but why does it throw an error at the same time?

encrypted output with error message enter image description here

Sinto
  • 3,915
  • 11
  • 36
  • 70
  • [Drop the pointless `.then(function (key) { return key; })`](https://stackoverflow.com/q/41089122/1048572) – Bergi Sep 03 '18 at 13:02
  • "*It is window.crypto.subtle.encrypt that throws the error*" - are you sure there? Given that it logs `encrypted`, that promise doesn't seem to get rejected. – Bergi Sep 03 '18 at 13:06
  • You are completly right, the issue was another place. However I have found out that Edge only implement a subset of the crypto.subtle – Rasmus Christiansen Sep 05 '18 at 10:20

0 Answers0