angel angel - 5 months ago 51
Node.js Question

Decrypt error using simplecrypt

I am new to nodejs,sails and need to encrypt some data before storing it in the database.

I have found the module simplecrypt

And have made this code demo.

var simplecrypt = require("simplecrypt");

var sc = simplecrypt();
var digest = sc.encrypt("my secret");
console.log(digest); // "66cea6eb1c18b8862485cf0604fa6062"

var message = sc.decrypt(digest);
console.log(message); // "my secret"


It does encrypt the value, but when I attempt to decrypt I get an error:

crypto.js:202
var ret = this._handle.final();
^
Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
at Error (native)
at Decipher.Cipher.final (crypto.js:202:26)


Can someone suggest a way for me to correctly decrypt my encrypted value?

Answer

After spend all day for checking, I foud out the result. I have to define

var user1 ={
  method:methodinput,
  password:passwordinput,
  salt:saltinput
};

and assign values get from mydabase to user1 and declare

 var sc = simplecrypt(user1);
 var passEncrypt = sc.encrypt(passworduserinput);

then compare user.password with passEncrypt.