Search code examples
javascriptnode.jscryptojs

I'm getting 'bad decrypt' error In Node.js


const crypto = require('crypto');

class EncryptedDataClass {
  constructor(message) {
    this.algorithm = 'aes-256-cbc';
    this.initVector = crypto.randomBytes(16);
    this.message = message;
    this.securityKey = crypto.randomBytes(32);
  }

  encrypt() {
    const cipher = crypto.createCipheriv(this.algorithm, this.securityKey, this.initVector);
    let encryptedData = cipher.update(this.message, "utf-8", "hex");
    encryptedData += cipher.final("hex");
    return encryptedData;
  }

  decrypt() {
    const decipher = crypto.createDecipheriv(this.algorithm, this.securityKey, this.initVector);
    let decryptedData = decipher.update(this.message, "hex", "utf-8");
    decryptedData += decipher.final("utf-8");
    return decryptedData;
  }
}

const secureName = new EncryptedDataClass("850749d212e39c8e24aee37bbb43e3c1eaee69ea592eeaeb93da5c83437f64a0");
console.log(secureName.decrypt());

I created that key using encrypted function but I can't decode that key, I'm getting an error:

06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt

How can I fix this error?


Solution

  • As AKX mentioned, you're creating a new IV everytime and when decrypting, you need the original IV. See my solution below. I'm storing the IV and later retrieving it by splitting the string on the "."

    const crypto = require("crypto");
    const algorithm = "aes-192-cbc"; // algorithm to use
    const password = 'defineaPassword';
    const key = crypto.scryptSync(password, 'salt', 24); // create key
    const iv = crypto.randomBytes(16); // generate different ciphertext everytime
    
    module.exports = {
        encryptAndBase64 (messagetext) {
            try {
                const iv = crypto.randomBytes(16); // generate different ciphertext everytime
                const cipher = crypto.createCipheriv(algorithm, key, iv);
                const ciphertext = cipher.update(messagetext, 'utf8', 'hex') + cipher.final('hex'); // encrypted text
                return `${iv.toString('base64')}.${ciphertext}`;
            } catch (e) {
                console.log(`error while encrypting:  ${e.message}`);
                return messagetext;
            }
        },
        decrypt (messagetext) {
            const split = messagetext.split('.');
            const iv = split[0];
            messagetext = split[1];
            const decipher = crypto.createDecipheriv(algorithm, key, Buffer.from(iv, 'base64'));
            return decipher.update(messagetext, 'hex', 'utf8') + decipher.final('utf8'); // deciphered text
        },
    };