1
Hi, I’m trying to make an AES-256-cbc encryption, and I’m having trouble creating a Key and an IV... I tried to use
crypto.randomBytes(32)
crypto.randomBytes(16)
32 to key, and 16 to IV. That way encrypt_token works smoothly, but decrypt_token error:
internal/crypto/cipher.js:174
const ret = this[kHandle].final();
^
Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
at Decipheriv.final (internal/crypto/cipher.js:174:29)
at decrypt_token (/home/joao/test.js:95:16)
at Object.<anonymous> (/home/joao/test.js:101:29)
at Module._compile (internal/modules/cjs/loader.js:1118:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1138:10)
at Module.load (internal/modules/cjs/loader.js:982:32)
at Function.Module._load (internal/modules/cjs/loader.js:875:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
library: 'digital envelope routines',
function: 'EVP_DecryptFinal_ex',
reason: 'bad decrypt',
code: 'ERR_OSSL_EVP_BAD_DECRYPT'
}
Follows the code:
var crypto = require('crypto'),
key = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
iv = 'AAAAAAAAAAAAAAAA';
function encrypt_token(data) {
var encipher = crypto.createCipheriv('aes-256-cbc', key, iv),
buffer = Buffer.concat([
encipher.update(data),
encipher.final()
]);
return buffer.toString('base64');
}
function decrypt_token(data) {
var decipher = crypto.createDecipheriv('aes-256-cbc', key, iv),
buffer = Buffer.concat([
decipher.update(Buffer.from(data, 'base64')),
decipher.final()
]);
return buffer.toString();
}
console.log('encriptado: ', encrypt_token('dmyz.org'));
console.log('decriptado: ', decrypt_token('FSfhJ/gk3iEJOPVLyFVc2Q=='));
Att.