简体   繁体   中英

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>

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM