title: node-rsa errors when trying to decrypt message with private key
body: <p>So I've been trying to use node with <a href="https://www.npmjs.com/package/node-rsa" rel="noreferrer">node-rsa</a> and javascript with <a href="http://travistidwell.com/jsencrypt/#" rel="noreferrer">jsencrypt</a> to create a website (for an assignment) where the javascript client gets the public key generated by the server (node-rsa), encrypts the message (jsencrypt) that the user has entered, sends it to the server and gets the server to decrypt it (node-rsa). The generation of the keys works, the encryption works however the decryption doesn't. When I start the node script I do the following for the encryption...</p>
  
  <pre class="lang-js prettyprint-override"><code>var NodeRSA = require('node-rsa');
  var myDecrypter = new NodeRSA({b: 512});
  </code></pre>
  
  <p>When the client requests the key (I am using express) the following is ran.</p>
  
  <pre class="lang-js prettyprint-override"><code>app.get('/getPublicKey', function(req, res){
      var publicKeyJson = {"Key": ""};
      console.log(myDecrypter.exportKey('public'));
      publicKeyJson.Key = myDecrypter.exportKey('public');
      res.json(JSON.stringify(publicKeyJson));
  });
  </code></pre>
  
  <p>The client then saves that key like this...</p>
  
  <pre class="lang-js prettyprint-override"><code>var myEncrypter = new JSEncrypt();
  var myJson  = "";
  $.getJSON( "getPublicKey", function( data ) {
      myJson = JSON.parse(data).Key;
          setKey();
  });
  function setKey() {
      myEncrypter.setPublicKey(myJson);
  }
  </code></pre>
  
  <p>When I got to encrypt and send the message on the client I do this...</p>
  
  <pre class="lang-js prettyprint-override"><code>function messageEncrypt() {
      message = document.getElementById("message").value;
      var encrypted = myEncrypter.encrypt(message);
      myMessage = {"username": "", "userId": 0.0, "message": ""};
      myMessage.username = me.username;
      myMessage.userId = me.userId;
      myMessage.message = encrypted;
      console.log(encrypted);
      $.post("sendMessage", myMessage);
  }
  </code></pre>
  
  <p>When the server receives a message this is what happens, this is where I get the errors. </p>
  
  <pre class="lang-js prettyprint-override"><code>app.post('/sendMessage', function(req, res){
      var message = req.body;
      var user = message.username;
      var id = message.userId;
      console.log("What a mess, " + user + " said " + message.message + " what on earth does that mean");
      //This line below errors
      var clearMessage = myDecrypter.decrypt(message.message, 'utf8');
      console.log(user + " said " + clearMessage);
  });
  </code></pre>
  
  <p>The error I get is ...</p>
  
  <pre><code>Error: Error during decryption (probably incorrect key). Original error: Error: error:040A1079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error
      at Error (native)
      at NodeRSA.module.exports.NodeRSA.$$decryptKey (/home/node_modules/node-rsa/src/NodeRSA.js:295:19)
      at NodeRSA.module.exports.NodeRSA.decrypt (/home/node_modules/node-rsa/src/NodeRSA.js:243:21)
      at /home/securechat/securechat.js:36:36
      at Layer.handle [as handle_request] (/home/node_modules/express/lib/router/layer.js:95:5)
      at next (/home/node_modules/express/lib/router/route.js:131:13)
      at Route.dispatch (/home/node_modules/express/lib/router/route.js:112:3)
      at Layer.handle [as handle_request] (/home/node_modules/express/lib/router/layer.js:95:5)
      at /home/node_modules/express/lib/router/index.js:277:22
      at Function.process_params (/home/node_modules/express/lib/router/index.js:330:12)
  </code></pre>
  
  <p>Here however is where it gets interesting, to get that error message above I had a private key of...</p>
  
  <pre><code>-----BEGIN RSA PRIVATE KEY-----
  MIIBOgIBAAJBAIhdx31QICGN1LKRW4WngeL3RtzPh7cEHmhFJB8m4bQUSTcSi4eg
  sUvMeZkWyaF9gOxtZKzk5TI6q+8hg8TY6S8CAwEAAQJASds423cVH/c4NsqhXh8e
  KvYwjBFeeNIjQegIq1KctbHmKNM5MMb4jnDqdY/S5XHHS22EGvLNheLgV8tlRjwG
  UQIhANpNmbl215eOsGPJ0jqz1XPMBrO35V6I3P04kvr66R1JAiEAn+oL0jtAFETR
  4PRfenye5MAu9US3V5MoDN8xUoEvKrcCIQDQT2ZWNNIrHAyzXB2QyJPxqInoqp1j
  5QPDWl3ewtj5iQIgY3E1nKw/stsA8LTGUvMAFBv2l4r9wDXAaBC7KSUwYY0CIAj4
  0gA9etDbPm3H/XDwK4WXs9mXkKroyxewkWoOoAw/
  -----END RSA PRIVATE KEY-----
  </code></pre>
  
  <p>and the public key sent to the client was...</p>
  
  <pre><code>-----BEGIN PUBLIC KEY-----
  MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIhdx31QICGN1LKRW4WngeL3RtzPh7cE
  HmhFJB8m4bQUSTcSi4egsUvMeZkWyaF9gOxtZKzk5TI6q+8hg8TY6S8CAwEAAQ==
  -----END PUBLIC KEY-----
  </code></pre>
  
  <p>The encrypted messages (stackoverflow) was ... </p>
  
  <pre><code>XDViV0InCSnpyBxbNu5Herut0JYSsp87buvhzM4g2f9z3khIx2zA8Ou0Uq0TtmqtvBBVtZi5wZbcS6em/vB78g==
  </code></pre>
  
  <p>The interesting thing is that when I used the <a href="http://travistidwell.com/jsencrypt/demo/index.html" rel="noreferrer">demo</a> on jsencrypt website and enter my private key as well as the encrypted message I get the correct decrypted message. </p>
  
  <p>So my question is...</p>
  
  <p>What am I doing wrong with my node-rsa decryption???</p>
  
  <p>If you need anymore information/code please put it in the comments below.</p>