Decryption with Node.js crypto module does not result in the encrypted text

63 views Asked by At

I am using AES-256-CFB encryption method to encrypt text but when decrypting using the same parameters it doesn't give the text I encrypted

Code used to encrypt

const ss = Buffer.from(decipher.update(sender.ss, "hex", "hex"), "hex");
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(process.env.encryptionMethod, ss, iv);
const encryptedMsg = cipher.update(req.body.msg, "utf8", "utf8") + cipher.final("utf8");

Code to Decrypt

const decipherMsg = crypto.createDecipheriv(process.env.encryptionMethod, ss, Buffer.from(msg.iv, "hex"));
const msgText = decipherMsg.update(msg.msg.toString("utf8"), "utf8", "utf8") + decipherMsg.final("utf8");

I have made sure that the key (ss) and initial vector (iv) are same in both cases. The following is an example:

  • req.body.msg: hi
  • encryptedMsg: �D
  • Buffer of encryptedMsg: <Buffer ef bf bd 44>

  • msg.msg: <Buffer ef bf bd 44>
  • msg.msg.toString("utf8"): �D
  • msgText: >�b�
0

There are 0 answers