用crypto nodejs解密大文件

问题描述 投票:1回答:1

我正在写一个大文件(450mb)。

我正在使用fs.createReadStream读取文件并使用crypto-js进行解密。

该文件已在UTF8中加密。

该文件的内容是JSON。

我的功能:

function decryptFile(srcDir, fileName, destDir) {

    let encryptedPath = path.join(srcDir, fileName);
    let decryptedPath = path.join(destDir, fileName).replace('.xam', '.json');

    console.log('DECRYPTING XAM FILE ' + encryptedPath + ' TO ' + decryptedPath);

    const input = fs.createReadStream(encryptedPath);

    input.once('readable', () => {

        const decipher = crypto.createDecipher('xxx-xxx-xxx', 'XxxX');

        const output = fs.createWriteStream(decryptedPath);

        input.pipe(decipher).pipe(output).on('finish', () => {

            console.log('FILE DECRYPTED');

        }).on('error', error => {

            console.log(error);

        });

    });
}

更新错误:

Error: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
    at Decipher._flush (crypto.js:158:28)
    at Decipher.prefinish (_stream_transform.js:137:10)
    at emitNone (events.js:106:13)
    at Decipher.emit (events.js:208:7)
    at prefinish (_stream_writable.js:602:14)
    at finishMaybe (_stream_writable.js:610:5)
    at afterWrite (_stream_writable.js:464:3)
    at onwrite (_stream_writable.js:454:7)
    at Decipher.afterTransform (_stream_transform.js:90:3)
    at Decipher._transform (crypto.js:153:3)

更新标题

javascript node.js cryptojs
1个回答
0
投票

我已经实现了相同的功能来模拟你的问题。我得到了同样的错误。你遇到了一个已知的问题。请遵循本指南。 http://vancelucas.com/blog/stronger-encryption-and-decryption-in-node-js/它的作品。测试了它。

const crypto2 = require('crypto');
var fs = require('fs');


function decryptFile(fileName) {

    const input = fs.createReadStream(fileName+'.encrypted');
    const output = fs.createWriteStream(fileName+'.unencrypted');


        const initVect = crypto2.randomBytes(16);
        const CIPHER_KEY = new Buffer('12345678901234567890123456789012');
        const decipher =  crypto2.createDecipheriv('aes-256-cbc', CIPHER_KEY, initVect);


        input.pipe(decipher).pipe(output).on('finish', () => {

            console.log('FILE DECRYPTED');

        }).on('error', error => {

            console.log(error);

        });
}

function encryptFile(fileName) {

    const initVect = crypto2.randomBytes(16);
    const CIPHER_KEY = new Buffer('12345678901234567890123456789012');


    var aes = crypto2.createCipheriv('aes-256-cbc', CIPHER_KEY, initVect);

    const input = fs.createReadStream(fileName);
    const output = fs.createWriteStream(fileName+'.encrypted');

    input 
      .pipe(aes)  
      .pipe(output)  
      .on('finish', function () {  
        console.log('done encrypting');
      });
} 

encryptFile('cas_01.cas');
//decryptFile('cas_01.cas');  
© www.soinside.com 2019 - 2024. All rights reserved.