javascript - Crypto-js returns different values every time it's run when using AES -
i'm trying encrypt using crypto-js , using aes type of encryption.
the problem i'm having encrypted value different every time encrypt it.
with simple example, run same encryption 5 different times , 5 different results. wtf 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());
check contents of aes.encrypt(secret, key)
- object number of fields, iv
, salt
of particular interest (jsfiddle).
each time run aes.encrypt
crypto-js chooses new iv , new salt (you can supply own values, way). random iv means output different same key, , random salt means actual encryption key different too, because derived the passphrase , salt.
you may (actually, should) ask why first ten base64 output characters same when both encryption key , iv different? because calling tostring()
on ecnryption result converts "openssl-compatible string", base64("salted__" + salt + ciphertext)
, "salted__"
constant prefix which, of course, leads same prefix in base64 output.
Comments
Post a Comment