I'm trying to encrypt some text using window.crypto
:
await crypto.subtle.encrypt(algorithm, key, dataArrayBuffer).catch(error => console.error(error));
However I get this error AES key data must be 128 or 256 bits
. I'm using PBKDF2 to create a 256 bit key from a password and I specify a key length of 256
:
window.crypto.subtle.deriveKey(
{
"name": "PBKDF2",
"salt": salt,
"iterations": iterations,
"hash": hash
},
baseKey,
{"name": "AES-GCM", "length": 256}, //<------------
true,
["encrypt", "decrypt"]
);
But I end up getting this key edi5Fou4yCdSdx3DX3Org+L2XFAsVdomVgpVqUGjJ1g=
after I exportKey
it and convert it from an ArrayBuffer
to a string
with a length of 44
bytes and 352
bits...
Which would explain the error, but how can I create an actual 256
bit key from window.crypto
's PBKDF2
?
JSFiddle: https://jsfiddle.net/6Lyaoudc/1/