при расшифровке с неверным ключом в node js приложение падает, даже если оно правильно включено в try catch - PullRequest
0 голосов
/ 08 марта 2020

Приложение работает нормально, когда я использую правильный ключ дешифрования, но когда я использую неправильный ключ дешифрования, происходит сбой всего приложения. Это блок кода, который я использую для извлечения данных из файла, дешифрования и отправки его в ответе.

try{
  const readIv = fs.createReadStream(filePath, { end: 15 });
  let initVect;
  readIv.on('data', (chunk) => {
  initVect = chunk;
  });

  readIv.on('close', () => {
    try{
    const readStream = fs.createReadStream(filePath, { start: 16 });
    const cipherKey = getCipherKey(key);
    const decipher = crypto.createDecipheriv('aes256', cipherKey, initVect);
    const unzip = zlib.createUnzip();
    res.set('Content-disposition', 'attachment; filename=' + file.name);
    res.set('Content-Type', file.format);
    var data;
    try{
      var data=readStream.pipe(decipher)
      //var decrypted = Buffer.concat([decipher.update(readStream), decipher.final()]);
     console.log("data ",data);
    }
    catch(exp)
    {
      console.log("exception while decrypting ",exp);
      res.status(500).json({message:"exception while decrypting the file, please add the correct key"});
    }
    try{
     data.pipe(unzip).pipe(res)
    }
    catch(exp)
    {
      console.log("exception while decrypting ",exp);
      res.status(500).json({message:"exception while decrypting the file, please add the correct key"});
    }
    }
    catch(exp)
    {
      res.status(500).json({message:"exception while decrypting the file, please add the correct key"});

    }

  });

}
catch(exp)
{
  res.status(500).json({message:"exception while decrypting the file, please add the correct key"});
}

И это ответ об ошибке, который я получаю, я думаю, это связано с передачей неверного потока данных в распакованный формат.

events.js:187
      throw er; // Unhandled 'error' event
      ^

Error: incorrect header check
    at Zlib.zlibOnError [as onerror] (zlib.js:170:17)
Emitted 'error' event on Unzip instance at:
    at errorOrDestroy (internal/streams/destroy.js:128:12)
    at Unzip.onerror (_stream_readable.js:783:9)
    at Unzip.emit (events.js:210:5)
    at Zlib.zlibOnError [as onerror] (zlib.js:173:8) {
  errno: -3,
  code: 'Z_DATA_ERROR'
}
...