Error while trying to encrypt

When I try to create encrypted text from text using crypto (Node.JS), I get the message "Error: must give an encryption key."

The code is as follows.

var cipher = crypto.createCipher('aes-256-cbc', userId);
var crypted = cipher.update(password, 'utf8', 'hex');
crypted += cipher.final('hex');
return crypted;

But when I test it with mocha, it gives no errors. In both cases, the inputs are given correctly. Can someone help me?

+4
source share
1 answer

Ok I changed userId to userId.toString ('binary') and it works now. Still not sure how the difference between starting mocha and normal starting occurs.

var cipher = crypto.createCipher('aes-256-cbc', userId);

changed to

var cipher = crypto.createCipher('aes-256-cbc', userId.toString('binary'));
+3
source

Source: https://habr.com/ru/post/1544615/


All Articles