Javascript - Best way to encrypt data with password

前端 未结 3 667
庸人自扰
庸人自扰 2021-02-10 07:08

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

相关标签:
3条回答
  • 2021-02-10 07:40

    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.

    0 讨论(0)
  • 2021-02-10 07:43

    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 讨论(0)
  • 2021-02-10 07:45

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

    What encryption algorithm is best for encrypting cookies?

    0 讨论(0)
提交回复
热议问题