Problem with streams and crypto

I tried to break the following code into 2 functions: First encryption, the second one decryption, this way:

Code:
var crypto = require('crypto'),
    algorithm = 'aes-256-ctr',
    password = 'd6F3Efeq';

var fs = require('fs');
var zlib = require('zlib');

// input file
var r = fs.createReadStream('file.txt');
// zip content
var zip = zlib.createGzip();
// encrypt content
var encrypt = crypto.createCipher(algorithm, password);
// decrypt content
var decrypt = crypto.createDecipher(algorithm, password)
// unzip content
var unzip = zlib.createGunzip();
// write file
var w = fs.createWriteStream('file.out.txt');

// start pipe
r.pipe(zip).pipe(encrypt).pipe(decrypt).pipe(unzip).pipe(w);

Code with 2 functions:
Code:
function encrypt(){

var crypto = require('crypto'),
    algorithm = 'aes-256-ctr',
    password = 'd6F3Efeq';

var fs = require('fs');
var zlib = require('zlib');


var r = fs.createReadStream('file.txt');
var zip = zlib.createGzip();
var encrypt = crypto.createCipher(algorithm, password);
var w = fs.createWriteStream('file.out.txt');

r.pipe(zip).pipe(encrypt).pipe(w)
}

function decrypt(){

var crypto = require('crypto'),
    algorithm = 'aes-256-ctr',
    password = 'd6F3Efeq';

var fs = require('fs');
var zlib = require('zlib');

var r = fs.createReadStream('file.out.txt');
var decrypt = crypto.createDecipher(algorithm, password)
var unzip = zlib.createGunzip();
var w = fs.createWriteStream('file1.txt');

r.pipe(decrypt).pipe(unzip).pipe(w);
}

But it seems that the decryption one always gives an error(And no matter what, it's always just "Script error") and writes an empty file. What's wrong with it? The original code works fine, but separating for some reason is breaking it...

EDIT:
Nah forget it. Found that I needed to call
Code:
var zip = zlib.createGzip();
var encrypt = crypto.createCipher(algorithm, password);
Inside the decrypt function too...

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/problem-with-streams-and-crypto.103338/