title: Encryption and Decryption in Node.js
body: <p>I am trying to encrypt and correspondingly decrypt a string.<br>
  <br>
  When I am specifying the encoding scheme as '<strong>utf-8</strong>', I am getting the results as expected :</p>
  
  <pre><code>    function encrypt(text) {
          var cipher = crypto.createCipher(algorithm, password)
          var crypted = cipher.update(text, 'utf8', 'hex')
          crypted += cipher.final('hex');
          return crypted;
      }
  
      function decrypt(text) {
          var decipher = crypto.createDecipher(algorithm, password)
          var dec = decipher.update(text, 'hex', 'utf8')
          dec += decipher.final('utf8');
          return dec;
      }
  
  //text = 'The big brown fox jumps over the lazy dog.'  
  </code></pre>
  
  <blockquote>
    <p>Output : (utf-8 encoding)</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/73Lmh.png" rel="noreferrer"><img src="https://i.stack.imgur.com/73Lmh.png" alt="enter image description here"></a></p>
  
  <p>But when I am trying to do it with '<strong>base-64</strong>', its giving me unexpected results :</p>
  
  <pre><code>function encrypt(text) {
      var cipher = crypto.createCipher(algorithm, password)
      var crypted = cipher.update(text, 'base64', 'hex')
      crypted += cipher.final('hex');
      return crypted;
  }
  
  function decrypt(text) {
      var decipher = crypto.createDecipher(algorithm, password)
      var dec = decipher.update(text, 'hex', 'base64')
      dec += decipher.final('base64');
      return dec;
  }
  </code></pre>
  
  <blockquote>
    <p>Output : (base-64 encoding)</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/v8H0Y.png" rel="noreferrer"><img src="https://i.stack.imgur.com/v8H0Y.png" alt="enter image description here"></a></p>
  
  <p><br>
  I am unable to understand why base-64 encoding scheme is not accepting up the spaces and '.' in right format.<br>
  If someone know this, please help me out in getting a better understanding for this.
  Any help is appreciated.</p>