I am trying to decrypt an encrypted text. If I provide the correct encrypted text, it works fine. But if I put the wrong encrypted text, the node app just crashes. No try catch block helps and no error is thrown.
Any what can I do to catch the error and return a graceful 500 error.
app.post("/decrypt", (req, res) => {
const key = crypto.scryptSync(password, "salt", 24);
const iv = Buffer.alloc(16, 0);
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = "";
decipher.on("readable", () => {
while (null !== (chunk = decipher.read())) {
decrypted += chunk.toString("utf8");
}
});
decipher.on("end", () => res.json({ decrypted }));
decipher.write(req.body.payload, "hex");
decipher.end();
});