Home > database >  I'am Getting 'bad decrypt' error In Node.js
I'am Getting 'bad decrypt' error In Node.js

Time:10-24

const crypto = require('crypto')
 class encryptedDataClass {
   constructor(massage){
     this.algorithm = 'aes-256-cbc'
     this.initVector = crypto.randomBytes(16);
     this.massage = massage;
     this.Securitykey = crypto.randomBytes(32);
   }

   encrypted(){
     const cipher = crypto.createCipheriv(this.algorithm, this.Securitykey, this.initVector);
     let encryptedData = cipher.update(this.massage, "utf-8", "hex");
     return encryptedData  = cipher.final("hex");
  }
  decrypted(){
    const decipher = crypto.createDecipheriv(this.algorithm, this.Securitykey, 
    this.initVector);
    let decryptedData = decipher.update(this.massage, "hex", "utf-8");
    return decryptedData  = decipher.final("utf-8");
 }

 }
 const secureName = new 
 encryptedDataClass("850749d212e39c8e24aee37bbb43e3c1eaee69ea592eeaeb93da5c83437f64a0") 
 console.log(secureName.decrypted())

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?

CodePudding user response:

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
    },
};
  • Related