Javascript - 用密码加密数据的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5782899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript - Best way to encrypt data with password
提问by Van Coding
I'm creating a server which can store cookies on the web that will contain application settings. The server will accept any data, but I want to encrypt all the settings before storing them in a cookie and decrypt them when reading them out. So I can store very sensitive data like Account Usernames & Passwords in the cookies and the server cannot do anything with it.
我正在创建一个服务器,它可以在网络上存储包含应用程序设置的 cookie。服务器将接受任何数据,但我想在将它们存储在 cookie 之前加密所有设置并在读取它们时解密它们。因此,我可以在 cookie 中存储非常敏感的数据,例如帐户用户名和密码,而服务器无法对其进行任何操作。
My question is now: What is the best way to encrypt such data with a password in JavaScript on the client side? What is the most secure?
我现在的问题是:在客户端使用 JavaScript 密码加密此类数据的最佳方法是什么?什么是最安全的?
I need some code that I can embed into my site and use it from there.
我需要一些可以嵌入我的网站并从那里使用的代码。
回答by WhiteFang34
I'd recommend using AES encryption in your JavaScript code. See Javascript AES encryptionfor libraries and links. The trouble you'll have is picking a key that is only available on the client side. Perhaps you can prompt the user? Or hash together some client system information that's not sent to the server.
我建议在您的 JavaScript 代码中使用 AES 加密。请参阅库和链接的Javascript AES 加密。您将遇到的麻烦是选择仅在客户端可用的密钥。也许你可以提示用户?或者将一些未发送到服务器的客户端系统信息散列在一起。
回答by John Hartsock
You could try a Vernam Cypherwith the password.
您可以使用密码尝试Vernam Cypher。
Basically you use the password as the key and then XOR the string to encrypt with the password. This can be reversed as well.
基本上,您使用密码作为密钥,然后对字符串进行异或以使用密码进行加密。这也可以反过来。
Here is a wikipedia page this type of encryption http://en.wikipedia.org/wiki/XOR_cipher
这是一个维基百科页面这种类型的加密http://en.wikipedia.org/wiki/XOR_cipher
Example Code
示例代码
function encrypt(key, value) {
var result="";
for(i=0;i<value.length;++i)
{
result+=String.fromCharCode(key[i % key.length]^value.charCodeAt(i));
}
return result;
}
function decrypt()
{
var result="";
for(i=0;i<value.length;++i)
{
result+=String.fromCharCode(key[i % key.length]^value.charCodeAt(i));
}
return result;
}
I haven't tested this but its probably close. you will notice the encrypt and decrypt functions should be identical
我还没有测试过这个,但它可能很接近。您会注意到加密和解密功能应该是相同的
回答by Hubro
EDIT: Misunderstood your question. Check out this question instead:
编辑:误解了你的问题。看看这个问题: