1

I am trying to encrypt some data as shown below:

let cipherText = CryptoJS.AES.encrypt(phrase, this.encryptionKey, {iv: initializationV});

But when I check the IV after encryption by using cipherText.iv, it does not match the IV that I used.

I am generating the IV as given below:

let initializationV = CryptoJS.lib.WordArray.random(16);

What is the issue over here?

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
  • Please show the code which you use to compare the IV values. – Maarten Bodewes Aug 21 '18 at 14:07
  • console.log(cipherText.iv.toString() === initializationV.toString()); – user3467098 Aug 21 '18 at 15:03
  • Hu, `===` is rather dangerous in case the type is just ever so slightly different types. Could you log the IV's themselves as a string and see what you get then? – Maarten Bodewes Aug 21 '18 at 15:05
  • Yeah. I did that and they turn out to be different. – user3467098 Aug 21 '18 at 15:07
  • *How* are they different? Could you show the values? Remember, we cannot see your screen! Most mistakes on crypto are encoding mistakes. – Maarten Bodewes Aug 21 '18 at 15:07
  • I just resolved it. Its just that when you are trying to encrypt/decrypt and you want to use your own IV, the key has to be a WordArray. It cannot be in plaintext. I found the answer here: https://stackoverflow.com/questions/35472396/how-does-cryptojs-get-an-iv-when-none-is-specified – user3467098 Aug 21 '18 at 20:16
  • You're welcome. I see that the answer from ArtjomB there doesn't **directly** say that the IV needs to be a word array, so feel free to answer your own question. It might be useful for future visitors. – Maarten Bodewes Aug 21 '18 at 20:19

0 Answers0