I want to use Web Cryptography API on an already existing project. To encrypt and decrypt something I have to use an CryptoKey, but when I save to CryptoKey into localStorage it only saves the String (CryptoKey) instead of the object.
Is it possible to serialize / convert an CryptoKey in an simple type (string)?
My decrypting method is
function decryptDataWithAES(keyName)
{
var decrypt_promise;
var aesKey = localStorage.getItem(keyName + 'key')
var item = localStorage.getItem(keyName)
var invokeVektor = localStorage.getItem(keyName + 'vector')
console.log("aesKey", aesKey )
crypto.subtle.decrypt({ name: "AES-CBC", iv: invokeVektor }, aesKey, item).then(function (result) {
decrypted_data = new Uint8Array(result); decrypted_data = new Uint8Array(result);
decrypt_promise = convertArrayBufferViewtoString(decrypted_data);
console.log('decryptDataWithAES ' + decrypt_promise);
return decrypt_promise;
},
function(e){
console.log(e.message);
}
);
}
The error message is of course:
Failed to execute 'decrypt' on 'SubtleCrypto': parameter 2 is not of type 'CryptoKey'. 2localStorageHandler.js:39 CryptoPromise[object CryptoKey]
If I decrypt without using localStorage there is no problem with encrypting the data.