Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crypto-js returns different values every time it's run when using AES

I'm trying to encrypt something using crypto-js and using the AES type of encryption.

The problem i'm having is that my encrypted value is different every time I encrypt it.

With this simple example, I run the same encryption 5 different times and I get 5 different results. Wtf is going on here?

task.js

var AES = require('crypto-js/aes');
var key = "abc123";
var secret = "encryptThisWord";

console.log(AES.encrypt(secret, key).toString());
console.log(AES.encrypt(secret, key).toString());
console.log(AES.encrypt(secret, key).toString());
console.log(AES.encrypt(secret, key).toString());
console.log(AES.encrypt(secret, key).toString());

enter image description here

like image 370
Catfish Avatar asked Jun 27 '14 15:06

Catfish


1 Answers

I faced the same issue. This is simply due to us not knowing the working of algorithm. Simply put, the key and IV are different for each call of the encrypt method, as mentioned in the above answer.

To ensure the exact same value for each iteration - you can refer to this answer https://stackoverflow.com/a/47096284/4098272

Alternatively, you can use the SHA3 function and compare the two Hash values.

like image 170
Jonathan Cardoz Avatar answered Sep 20 '22 14:09

Jonathan Cardoz