最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

encryption - Javascript - Best way to encrypt data with password - Stack Overflow

programmeradmin5浏览0评论

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.

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?

I need some code that I can embed into my site and use it from there.

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.

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?

I need some code that I can embed into my site and use it from there.

Share Improve this question asked Apr 25, 2011 at 20:30 Van CodingVan Coding 24.5k25 gold badges92 silver badges137 bronze badges 1
  • 3 SSL... – Kevin Peno Commented Apr 25, 2011 at 20:35
Add a comment  | 

3 Answers 3

Reset to default 14

I'd recommend using AES encryption in your JavaScript code. See Javascript AES encryption for 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.

EDIT: Misunderstood your question. Check out this question instead:

What encryption algorithm is best for encrypting cookies?

You could try a Vernam Cypher with the password.

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

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

发布评论

评论列表(0)

  1. 暂无评论