Here's my code:
var key = 'aaaaaaaaaaaaaaaa'
var iv = 'bbbbbbbbbbbbbbbb';
var ciphertext = '10f42fd95857ed2775cfbc4b471bc213';
// from https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey#PKCS_8_import
function str2ab(str) {
const buf = new ArrayBuffer(str.length);
const bufView = new Uint8Array(buf);
for (let i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
key = new TextEncoder().encode(key);
iv = new TextEncoder().encode(iv);
ciphertext = str2ab(ciphertext);
window.crypto.subtle.importKey(
'raw',
key,
{
name: 'AES-CBC'
},
true, // can the key be extracted using SubtleCrypto.exportKey() / SubtleCrypto.wrapKey()?
['decrypt'] // keyUsages
).then(function(key) {
window.crypto.subtle.decrypt(
{
name: "AES-CBC",
iv: iv
},
key,
ciphertext
).then(function(plaintext) {
console.log(new TextDecoder().decode(plaintext));
})
});
When I run it I get Uncaught (in promise) Error in the JS Console.
Here's a JSFiddle showing the error:
https://jsfiddle.net/96gx7hz3/
Any ideas?