3

I am decripting a large file (450mb).

I am reading the file with fs.createReadStream and decrypting with crypto-js.

The file has been encrypted in UTF8.

The contents of the file is JSON.

MY FUNCTION:

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

        });

    });
}

UPDATE 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)

UPDATE Title

Max Ferreira
  • 679
  • 1
  • 5
  • 21

1 Answers1

3

I've implemented the same to simulate your problem. i got the same error. You have hit a known issue. please follow this guide. http://vancelucas.com/blog/stronger-encryption-and-decryption-in-node-js/ it works. tested it.

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');  
jcuypers
  • 1,774
  • 2
  • 14
  • 23
  • Sorry, I had not seen your implementation – Max Ferreira Feb 28 '19 at 19:44
  • I do my encryption in java, using AES/ECB/PKCS5Padding – Max Ferreira Feb 28 '19 at 19:52
  • didnt test it but here's the exact answer: https://stackoverflow.com/questions/50922462/aes-cbc-pkcs5padding-iv-decryption-in-nodejs-encrypted-in-java – jcuypers Feb 28 '19 at 19:55
  • I am used crypto.createDecipheriv('aes-128-ecb', CIPHER_KEY, initVect), as you said. But generated another error: Error: Invalid IV length – Max Ferreira Feb 28 '19 at 19:55
  • you need 32 characters there. i stripped a function. which was not provided anyway. you need to see how you can convert your password to your cipher key i guess. in my example i need 32 bytes/chars – jcuypers Feb 28 '19 at 19:56
  • 1
    check the last link. we/they all have the same issues. the length depends on the method. – jcuypers Feb 28 '19 at 19:58
  • Using these two function to crypt and decrypt a file I obtain a bad decryption. No error but when I open the decrypted file the firsts 9 characters is still encrypted. Indeed if I insert at the begin of my file 9 charachters, the rest of the file was decrypted correctly but there are the firsts chars encrypted. Any solution? – Edo2610 Apr 29 '22 at 08:38