Achraf.Davinci Achraf.Davinci - 2 months ago 7
Javascript Question

Can't print CryptoJS-encrypted ciphertext to page

I can't access the

encryptedData
variable with jQuery but in
alert
it works!!!

<script src="js/jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="js/aes.js"></script>
<script>
$( document ).ready(function() {
var textToEncrypt = "textToEncrypt";
var secretPhrase = "secretPhrase";
var encryptedData = CryptoJS.AES.encrypt(textToEncrypt, secretPhrase);
$('#data').text(encryptedData);
alert(encryptedData);
});
</script>

<div id="data"></div>

Answer

encryptedData is a CipherParams object containing various properties such as ciphertext, salt and iv. jQuery's text function takes this object and tries to assign it directly to the element. alert on the other hand first stringifies it. You probably want:

$('#data').text(encryptedData.toString());