0

I am encrypting some important content at client side using CryptoJS (AES). The code I am using is below:

   function encrypt(value) {
     var keyIV = 'Ei9sHWE25Jiol77Q';
     return CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(value), CryptoJS.enc.Utf8.parse(keyIV),
     {   keySize: 256 / 8,
        iv: CryptoJS.enc.Utf8.parse(keyIV),
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
     }).toString();
}

But as you see the key used to encrypt can be seen by anyone. How can I make this key secure or non human readable format or any other tricks to make sure the key is secure?

tjb1
  • 747
  • 9
  • 30
Suresh
  • 1
  • 1

2 Answers2

1

You need to give more details on what you're trying to do and what your concerns are.

For example:

  • if clients should also be able to decrypt the data they're encrypting, then public key cryptography is out of the question (unless clients ask the server to decrypt the data for them, but then what's the point of encryption if the server will readily decrypt everything for you?)
  • are you concerned about clients decrypting data of other clients? If so, can your server generate a different key for each client? Should the server be able to decrypt data of clients (and thus requires a copy of all keys)?

There is rarely an absolute right answer in security. There are different tools for different purposes.

YSK
  • 1,572
  • 10
  • 19
  • If I look at the OP’s code in Dev Tools, I can take his key and decrypt his encrypted data, by doing: const decrypted = CryptoJS.AES.decrypt(encrypted, key, { iv: iv }); console.log('decrypted: ', decrypted); const plaintext1 = decrypted.toString(CryptoJS.enc.Utf8); console.log('plaintext: ', plaintext1); What is the point of client side encryption, when the key cannot be secured? – Charles Robertson Feb 05 '22 at 11:11
-2

If you are only worry about the key, use a asymmetric encryption. You'll only need to store the public key at client side.

The encrypted data is save and cant by manipulated and you can decrypted it at server side with your private key.

Marcus
  • 1,910
  • 2
  • 16
  • 27