javascriptsjcl

SJCL library decryption issue


I'm having an issue when I attempt to do a simple decryption with the SJCL library. I can encrypt data fine.

In this example I have saved encrypted data like so:

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

I ajax the data off to a database. Now when I go to decrypt the data, I pull the sjcl JSON string from my database and run it through this function:

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

I get the following error on the console and it won't progress beyond that sjcl.decrypt statement.

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

Surely I'm doing something obviously wrong here?


Solution

  • I managed to solve my problem. Basically it was two/three issues. I needed to do a jquery "encodeURIComponent" on the parameter before I sent it to the php script doing the database insert, because the PHP extract function stripped the "+" in the encrypted string.

    The second issue was having to use PHP rawurldecode (as opposed to urldecode) to get my string back to pre ajax format with "+" chars, as opposed to " ".

    The third issue was using jquerys "JSON.stringify" to turn it from a javascript 'object' into a string that the sjcl.decrypt would work with. The original javascript error makes sense now!