我对构建一个个人使用的小型应用程序感兴趣,该应用程序将使用JavaScript在客户端加密和解密信息。加密的信息将存储在服务器上的数据库中,但绝不会存储解密的版本。
它不必是超级duper安全,但我想使用目前未被破坏的算法。
理想情况下,我可以做一些
var gibberish = encrypt(string, salt, key);
来生成编码的字符串,诸如此类
var sensical = decrypt(gibberish, key);
稍后再解码。
到目前为止,我看到的是:
http://bitwiseshiftleft.github.io/sjcl/
我还应该看看其他的图书馆吗?
2021年12月更新
使用MDN Web Docs提供的加密api:
https://developer.mozilla.org/en-US/docs/Web/API/Crypto
旧的答案
使用SimpleCrypto
使用encrypt()和decryption ()
要使用SimpleCrypto,首先使用
密钥(密码)。时必须定义密钥参数
创建一个SimpleCrypto实例。
要加密和解密数据,只需使用encrypt()和decrypt()
函数。这将使用AES-CBC加密算法。
var _secretKey = "some-unique-key";
var simpleCrypto = new SimpleCrypto(_secretKey);
var plainText = "Hello World!";
var chiperText = simpleCrypto.encrypt(plainText);
console.log("Encryption process...");
console.log("Plain Text : " + plainText);
console.log("Cipher Text : " + cipherText);
var decipherText = simpleCrypto.decrypt(cipherText);
console.log("... and then decryption...");
console.log("Decipher Text : " + decipherText);
console.log("... done.");