title: How to Properly Encrypt JSON String in NodeJS
body: <p>This data is the hex string of encrypted json string. This is encrypted by symmetric key.</p>
  
  <pre><code>Key: PojmoHD0HNuE/Q+3sKD/ARj86XRYitueWmpjyCuoHuSX71xXlU0hN72wXdrWdt7w//N1BF6GQMLKZIkKXpQ8v18vP1kFG/vO3TpKZRTPhGUyFxE8y1CRIPUJesnbM+Cy3xPegQ3H2wn00YP5f2Dw0uDUHke2MEkCXHYyAgxGqo8=
  
  Encrypted Hex: 2D31727668A7008B59552C7186C28D51C3E5A5A1DC90AF46577705A564D3CF1A3866BB4A79E5029EBFC8871CA769DC5EBD5599C040070B27464F158D3DEE50DD9BBF5E7B8CB31C0FEF453DB04A1B313A3918A9164C355A376A7866821277E47273CEC268FBD3ED2625E0518C6ED8C8F890C0A5642D7C7BD8B9C017D5E3E94B3EE58E14C79762BF21881A90636F3024186B27ED216C1FF6B9DF6887005546873A33683025616BDF1F283FB59DB686DC815D56047C50FB9B53735011E60057703F468E11C7D5975AC5B39D8FAA6865C67095A58C1F1CB048670E0AB6F9B818434BDF6B085F58906101FA885D0B8799C6F1600AAE98DE2651A731D519466D516C1B
  </code></pre>
  
  <p>I have to encrypt/decrypt JSON String in the following manner with the key. I tried different algorithms but none of them are working.
  I tried the following code.</p>
  
  <pre><code>const crypto = require('crypto');
  const algorithm = 'aes-256-cbc'
  
  const encryptData = (data, key) =&gt; {
  
      var mykey = crypto.createCipher(algorithm, key);
  
      var mystr = mykey.update(data, 'utf8', 'hex')
      mystr += mykey.final('hex');
      console.log(mystr);
      return mystr;
  }
  
  const decryptData = (encrypted, key) =&gt; {
  
      var mykey = crypto.createDecipher(algorithm, key);
      var mystr = mykey.update(encrypted, 'hex', 'utf8')
      mystr += mykey.final('utf8');
  
      console.log(mystr);
      return mystr;
  }
  </code></pre>