JavaScript字符串加密和解密? [英] JavaScript string encryption and decryption?
本文介绍了JavaScript字符串加密和解密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有兴趣构建一个个人使用的小应用程序,使用JavaScript对客户端的信息进行加密和解密。加密的信息将被存储在服务器上的一个数据库中,但从未被解密的版本。
I'm interested in building a small app for personal use that will encrypt and decrypt information on the client side using JavaScript. The encrypted information will be stored in a database on a server, but never the decrypted version.
它不一定要超级安全,但是我想使用当前不间断的算法。
It doesn't have to be super duper secure, but I would like to use a currently unbroken algorithm.
理想情况下,我可以做一些像
Ideally I'd be able to do something like
var gibberish = encrypt(string, salt, key);
生成编码的字符串,像
var sensical = decrypt(gibberish, key);
稍后解码。
我已经看到这个:
http://crypto.stanford.edu/sjcl/
我应该查看的任何其他库?
Any other libraries I should look at?
推荐答案
var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
//U2FsdGVkX18ZUVvShFSES21qHsQEqZXMxQ9zgHy+bu0=
var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");
//4d657373616765
document.getElementById("demo1").innerHTML = encrypted;
document.getElementById("demo2").innerHTML = decrypted;
document.getElementById("demo3").innerHTML = decrypted.toString(CryptoJS.enc.Utf8);
Full working sample actually is:
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
<br><br>
<label>encrypted</label>
<div id="demo1"></div>
<br>
<label>decrypted</label>
<div id="demo2"></div>
<br>
<label>Actual Message</label>
<div id="demo3"></div>
这篇关于JavaScript字符串加密和解密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文