title: Encrypting file in Node via Crypto and Stream
body: <p>I want to read from a stream then encrypt it and finally write it to another file.
  This is my code:</p>
  
  <pre><code>var fs = require('fs');
  var crypto = require('crypto');
  var infile = fs.createReadStream('a.dmg');
  var outfile = fs.createWriteStream('b.dmg');
  var encrypt = crypto.createCipher('aes192', 'behdad');
  var size = fs.statSync('a.dmg').size;
  console.log(size);
  infile.on('data',function(data) {
      var percentage = parseInt(infile.bytesRead) / parseInt(size);
      console.log(percentage * 100);
      var encrypted = encrypt.read(data);
      console.log(encrypted);
      if(encrypted){
          console.log(encrypted);
          outfile.write(encrypted);
      }
  
  
  });
  infile.on('close', function() {
      encrypt.end();
       outfile.close();
  
  });
  </code></pre>
  
  <p>But it returns an empty file, and <code>encrypted</code> is null. What is the problem? I don't want to use <code>pipe</code> .</p>