尝试使用SJCL库进行简单解密时遇到问题。我可以加密数据。

在此示例中,我保存了加密数据,如下所示:

encdata = sjcl.encrypt($('input[name="pass"]').val(), $('textarea[name="cleartxt"]').val());


我将数据保存到数据库中。现在,当我解密数据时,我从数据库中提取sjcl JSON字符串并通过以下函数运行它:

function decryptdata(encdata) {
    var dpassword = prompt('Decryption Password');
    console.log(sjcl.decrypt(dpassword, encdata));
    //$('.decrypted').html(dec);
}


我在控制台上收到以下错误,并且该错误不会超出该sjcl.decrypt语句。

sjcl.js:57 Uncaught TypeError: a.replace is not a function
at Object.decode (sjcl.js:57)
at Object.decrypt (sjcl.js:56)
at decryptdata (my.js:72)
at my.php?r=test:13


我肯定在这里做错了什么吗?

最佳答案

我设法解决了我的问题。基本上是两个/三个问题。在将参数发送到进行数据库插入的php脚本之前,我需要对参数执行jquery“ encodeURIComponent”,因为PHP提取函数会去除加密字符串中的“ +”。

第二个问题是必须使用PHP rawurldecode(而不是urldecode)将我的字符串恢复为带有“ +”字符而不是“”的pre ajax格式。

第三个问题是使用jquery“ JSON.stringify”将其从javascript“对象”转换为sjcl.decrypt可以使用的字符串。原始的javascript错误现在很有意义!

09-25 18:50