使用Node.js解密大文件

3

我正在解密一个大文件(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)

更新 标题


唯一让我不太清楚的是... 你所说的cryptojs,很可能是较新版本的node中内置的加密模块。 - jcuypers
是的,我的错... - Max Ferreira
您需要使用iv函数。我只是为了模拟而简化了很多内容,但它可以正常工作。 - jcuypers
1个回答

3

我已经实现了相同的模拟来重现你的问题。我遇到了相同的错误。你碰到了一个已知的问题,请按照此指南操作。它可以解决问题,并已通过测试。

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');  

抱歉,我没有看到你的实现。 - Max Ferreira
我使用Java进行加密,采用AES/ECB/PKCS5Padding算法。 - Max Ferreira
没有测试过,但这是确切的答案:https://dev59.com/76zka4cB1Zd3GeqP50JK - jcuypers
我使用了crypto.createDecipheriv('aes-128-ecb', CIPHER_KEY, initVect),就像你说的一样。但是出现了另一个错误:错误:无效的IV长度 - Max Ferreira
1
请检查最后一个链接。我们/他们都有相同的问题。长度取决于方法。 - jcuypers
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接