简体   繁体   中英

how to encrypt a string using javascript

I tried encrypting a string using javascript, and it worked very well. But 1 day after it worked it stopped working, and i dont understand why. Here's my code:

 alert(encrypt("hi", "123")); function encrypt(data, key) { return CryptoJS.AES.encrypt(data, key).toString(); } function decrypt(data, key) { return CryptoJS.AES.decrypt(data, key).toString(CryptoJS.enc.Utf8); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>

I tried running

alert(encrypt("hi", "123"));

to test if it worked, but no alert showed up. Can someone help me? Thanks!

You imported the aes package of CryptoJs and therefore the reference to CryptoJs does not exists.

Try to replace the import by <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>

Working example

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