0

This is a simple encryption/Decryption Controller I want condition when encryption-decryption failed then run else block otherwise go with if Block work completely fine but when encryption-decryption failed else block not works console logging an error

app.post("/playground3", async (req, res, next) => {
  try {
    const data = Data.findById(dataId).then((result) => {
      let key = result.enc_key;
      console.log(key);
      let iv = result.iv;
      console.log(iv);

      let encryptedText = Buffer.from(req.body.data, "base64");
      console.log(encryptedText);

      let decipher = crypto.createDecipheriv(
        "aes-256-cbc",
        Buffer.from(key),
        iv
      );
      if (!decipher) {
        // MY CODE...
        return res
          .status(200)
          .send({ success: true, encryptData, message: "Data pass IF BLOCK" });
      } else {
        // MY CODE
        res.status(404).send({ success: false, message: "Data Not Found" });
      }
    });
  } catch (error) {
    console.log(error);
  }
});
Smit Gajera
  • 1,001
  • 1
  • 8
  • 26
  • Maybe that error should be dealt with? – Dave Newton Oct 21 '21 at 12:22
  • ElseBlock code for that error – Smit Gajera Oct 21 '21 at 12:26
  • Getting encryption-decryption failed error so Else block regenerates key and iv and crypt-decryption. This is the concept – Smit Gajera Oct 21 '21 at 12:27
  • what is the error in console log? Seems to be an issue with the code written into else block, please share complete code or error you are getting – chetan mekha Oct 21 '21 at 13:42
  • Decrypt data with the old Key and IV (expired) and get an error at that time when I got this error I want to perform code that is in the else block. And If Key and Iv not expired If block works fine for me – Smit Gajera Oct 21 '21 at 13:47
  • 1
    Is it logging an error in the `catch` block? It's not clear what the error is because you haven't provided any information regarding it. – Dave Newton Oct 21 '21 at 13:54

0 Answers0