title: How to decrypt cookie with nodejs
body: <p>I am trying to make <a href="https://gist.github.com/troufster/2511828" rel="nofollow noreferrer">run this</a></p>
  
  <pre><code>function hex2a(hex) {
      var str = '';
      for (var i = 0; i &lt; hex.length; i += 2)
          str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
      return str;
  }
  
  //Raw cookie
  var cookie = "B417B464CA63FE780584563D2DA4709B03F6195189044C26A29770F3203881DD90B1428139088D945CF6807CA408F201DABBADD59CE1D740F853A894692273F1CA83EC3F26493744E3D25D720374E03393F71E21BE2D96B6110CB7AC12E44447FFBD810D3D57FBACA8DF5249EB503C3DFD255692409F084650EFED205388DD8C08BF7B941E1AC1B3B70B9A8E09118D756BEAFF25834E72357FD40E80E76458091224FAE8";
  
  //decryptionKey from issuers &lt;machineKey&gt;
  var deckey = "FFA87B82D4A1BEAA15C06F6434A7EB2251976A838784E134900E6629B9F954B7";
  
  
  var crypto = require('crypto');
  
  var ivc = cookie, iv, cipherText, ivSize = 16, res = "";
  
  ivc = new Buffer(ivc, 'hex');
  iv = new Buffer(ivSize);
  cipherText = new Buffer(ivc.length - ivSize);
  ivc.copy(iv, 0, 0, ivSize);
  ivc.copy(cipherText, 0, ivSize);
  
  c = crypto.createDecipheriv('aes-256-cbc', hex2a(deckey), iv.toString('binary'));
  res = c.update(cipherText, "binary", "utf8");
  res += c.final('utf8');
  
  
  console.log(res);
  </code></pre>
  
  <p>In this <a href="https://stackoverflow.com/q/21292142/492258">Q&amp;A</a>, it mentions about differences about node js versions, I tried that apply that one but with out success:</p>
  
  <pre><code>res = c.update(cipherText, "binary", "utf8");
  </code></pre>
  
  <p>line result such result</p>
  
  <pre><code>�sJ舸=�X7D������G����}x���T
  </code></pre>
  
  <p>and</p>
  
  <pre><code>res += c.final('utf8'); 
  </code></pre>
  
  <p>gives this error</p>
  
  <pre><code>0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
  </code></pre>
  
  <p>nodejs version: 4.1.2 and crypto version 0.0.3</p>
  
  <p>How can I properly decrypt cookie with this algorith or can you suggest any other?</p>