显示数字包络例程:EVP_DecryptFinal_ex:错误的最终块长度

时间:2018-03-07 09:57:00

标签: node.js aes

当我尝试使用下面给出的代码解密AES加密的视频文件时,

// generate random passphrase binary data
var r_pass = crypto.randomBytes(12);


// convert passphrase to base64 format
var key    = r_pass.toString("base64");

console.log("password:",key)


exports.encryptFile = function(key, inputFile, outputFile, algorithm = 'aes-256-cbc')
{
    var cipher    = crypto.createCipher('aes-256-cbc', key)
    var input     = fs.createReadStream(inputFile)
    var output    = fs.createWriteStream(outputFile)
    var startTime = Date.now()
    input.pipe(cipher).pipe(output)
    output.on('finish', function() {
      console.log('Encrypted file written to disk!');
      var endTime = Date.now();
      console.log("Time taken to encrypt "+ (endTime - startTime)/1000 + "Seconds")

    });
}

this.encryptFile(key,'./videos/20mb.mp4', './videos/20mb-encrypted.leg')

exports.decryptFile = function(key, inputFile, outputFile, algorithm = 'aes-256-cbc')
{
    var cipher    = crypto.createDecipher('aes-256-cbc', key)
    var input     = fs.createReadStream(inputFile)
    var output    = fs.createWriteStream(outputFile)
    var startTime = Date.now()
    input.pipe(cipher).pipe(output);

    output.on('finish', function() {
      console.log('Decrypted file written to disk!')
      var endTime = Date.now();
      console.log("Time taken to decrypt "+ (endTime - startTime)/1000 + "Seconds")
    });

}

this.decryptFile(key,'./videos/20mb-encrypted.leg', './videos/20mb-decrypted.mp4')

它返回错误,

[Wed Mar 07 2018 09:28:31 GMT+0000 (UTC)] ERROR Process crashed with uncaught exception: Error: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
at Error (native)
at Decipher._flush (crypto.js:133:28)
at Decipher.<anonymous> (_stream_transform.js:118:12)
at Decipher.g (events.js:292:16)
at emitNone (events.js:86:13)
at Decipher.emit (events.js:185:7)
at prefinish (_stream_writable.js:504:12)
at finishMaybe (_stream_writable.js:512:7)
at endWritable (_stream_writable.js:524:3)
at Decipher.Writable.end (_stream_writable.js:489:5)

我检查了代码但是我无法在流程中发现任何错误。我已经使用npm模块加密来加密和解密,但是我在尝试解密数据时才收到此错误。

0 个答案:

没有答案