0

Please help me resolve this problem.

var key = 'real secret keys should be long and random';

// Create an encryptor:
var encryptor = require('simple-encryptor')(key);

var encrypted = encryptor.encrypt('testing');
// Should print gibberish:
console.log('encrypted: %s', encrypted);

I'm using library: "simple-encryptor" on "NPM", but each time I run "encrypt" function, it will output difference result.

Ex:
1st: "4792f3eacff628801005f14f1bc25ba0353…3e969662c4i/It97adse8M+1tmRHnYCQ=="
2nd: "6c576df521df45cc48ffe594fbe13084353…66e3552bdaLoAV3rortuDbJYox1+lVWQ=="
and so on.
(You can run sample code on : https://npm.runkit.com/simple-encryptor )

Therefore: when I save data to local & come back I can't decrypt this data.

Why result of encrypts are different each time I run ?
How to decrypt data in this case ?

2 Answers2

0

Most encryption algorithms require an initialization vector (IV). The IV itself is not sensitive, and is typically saved alongside the output ciphertext. In some cases, the library will handle this for you and just include it as part of the output.

Presumably the IV is being randomly-generated by the library to protect you from problems due to key reuse. This randomly-generated IV will cause the output to be different every time you run your script.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
0

By default, simple-encryptor is going to use encrypt-then-mac with AES-256 and SHA-256 HMAC. Their docs say:

Unique IV per call so no two calls should return the same result for the same input

To decrypt the encrypted data, use encryptor.decrypt(encrypted);. You can test this with this simple example:

var encryptor = require("simple-encryptor")("somekey234567884456753456");

var encrypted = encryptor.encrypt('testing');

console.log(encrypted);

var decrypted = encryptor.decrypt(encrypted);

console.log(decrypted);
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44