我使用forge库来加密json字符串和LZString压缩字符串, 当我尝试加密常规字符串时,它可以工作。但是当我尝试加密json或压缩字符串时,我得到以下内容
URIError: URI malformed
at decodeURIComponent (<anonymous>)
at Object.o.decodeUtf8 (forge.min.js:formatted:2)
at n.o.ByteStringBuffer.toString (forge.min.js:formatted:2)
我的代码是:
function encrypt(content, password, keySize = 32) {
const iv = forge.random.getBytesSync(16);
const salt = forge.random.getBytesSync(keySize * 8);
const key = forge.pkcs5.pbkdf2(password, salt, 1, keySize);
const cipher = forge.cipher.createCipher('AES-CBC', key);
cipher.start({iv});
cipher.update(forge.util.createBuffer(content));
cipher.finish();
let result = {
data: cipher.output,
iv,
salt
};
decrypt(result, password);
}
function decrypt(encrypted, password) {
const key = forge.pkcs5.pbkdf2(password, encrypted.salt, 1, 16);
const decipher = forge.cipher.createDecipher('AES-CBC', key);
decipher.start({iv: encrypted.iv});
decipher.update(encrypted.data);
decipher.finish();
return decipher.output.toString(); // The line causes the error
}
decrypt(encrypt('{"key": "value"}', "password"), "password");
问题是什么?