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

3
  • The only thing which was not really clear to me ... by cryptojs, you mean most probably the built-in crypto modules in the newer versions of node Commented Feb 28, 2019 at 19:30
  • yes, my bad.... Commented Feb 28, 2019 at 19:33
  • you need to use the iv functions. I've simplified a lot just to mockup. but it works. Commented Feb 28, 2019 at 19:37

1 Answer 1

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');  
Sign up to request clarification or add additional context in comments.

7 Comments

Sorry, I had not seen your implementation
I do my encryption in java, using AES/ECB/PKCS5Padding
didnt test it but here's the exact answer: stackoverflow.com/questions/50922462/…
I am used crypto.createDecipheriv('aes-128-ecb', CIPHER_KEY, initVect), as you said. But generated another error: Error: Invalid IV length
check the last link. we/they all have the same issues. the length depends on the method.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.