title: node-rsa and openssl compatibility
body: <p>I have a set of public/private keys, that works flawless when encrypting/decrypting some data using only one of the 2 ways for both encryption and decryption.</p>
  
  <p>I still have no luck trying to encrypt the data with the one of the two and decrypt it with the other.</p>
  
  <p>Example schenario:</p>
  
  <p>a) I create some encrypted data using the public key with the following node.js code:</p>
  
  <pre><code>#!/usr/bin/env node
  
  var NodeRSA = require('node-rsa');
  var fs = require('fs');
  
  function createUsingPubKey(Pub, data) {
      var pk = new NodeRSA();
      pk.importKey(Pub);
      encrypted = pk.encrypt(data, 'base64');
      return encrypted;
  }
  
  var sampledata = "SECRET STUFF";
  
  var genkey = createUsingPubKey(fs.readFileSync('id_rsa.pub'), sampledata)
  console.log(genkey);
  </code></pre>
  
  <p>b) Then i try to decrypt it using openssl utility with this:</p>
  
  <pre><code>node test.js | openssl base64 -d -A | openssl rsautl -inkey id_rsa
  </code></pre>
  
  <p>But i get:</p>
  
  <pre><code>RSA operation error
  1068:error:0406706C:rsa routines:RSA_EAY_PUBLIC_DECRYPT:data greater than mod len:.\crypto\rsa\rsa_eay.c:680:
  </code></pre>
  
  <p>I thought that they probably would use different algorithms for the encryption/decryption procedure so i headed to the node-rsa documentation here:
  <a href="https://www.npmjs.com/package/node-rsa" rel="nofollow">https://www.npmjs.com/package/node-rsa</a> and i found this option:</p>
  
  <blockquote>
    <p>encryptionScheme — padding scheme for encrypt/decrypt. Can be
    'pkcs1_oaep' or 'pkcs1'. Default 'pkcs1_oaep'.</p>
  </blockquote>
  
  <p>I then tried to decrypt passing the -oaep option in openssl util like this:</p>
  
  <pre><code>node test.js | openssl base64 -d -A | openssl rsautl -oaep -inkey id_rsa
  </code></pre>
  
  <p>But i still get:</p>
  
  <pre><code>RSA operation error
  5216:error:0406706C:rsa routines:RSA_EAY_PUBLIC_DECRYPT:data greater than mod len:.\crypto\rsa\rsa_eay.c:680:
  </code></pre>
  
  <p>My knowledge on cryptography is really basic. Any help would be appreciated :)</p>
  
  <p>EDIT 1:
  The node.js module can be found here:
  <a href="https://github.com/rzcoder/node-rsa" rel="nofollow">https://github.com/rzcoder/node-rsa</a></p>
  
  <p>EDIT 2:
  As Maarten Bodewes requested, here is some sample data:</p>
  
  <p>Plaintext data to be encrypted:</p>
  
  <blockquote>
    <p>You're no good, you're no good, you're no good Baby, you're no good
    (I'm gonna say it again) You're no good, you're no good, you're no
    good Baby, you're no good</p>
  </blockquote>
  
  <p>A private key generated with </p>
  
  <pre><code>openssl genrsa -out key 512
  
  -----BEGIN RSA PRIVATE KEY-----
  MIIBOgIBAAJBALOUBygyX11BsDoEIKoZzn2/HAXPorNR/X8wCDaBlcPtOHxKAZFk
  Vra1+Pem1urtSlnEqc07DwAP6v0GEGHpxbkCAwEAAQJAGAZ17qrOl2tyaFClDhzl
  w20OErj0y4jsoVeLwb8UimG48JslS14hfM9XxE/fG6qypN8u7LUhlnBC68ZcQ9Jg
  AQIhAORaVlB7trWp6n7dETvdY9J2p8ubOuyLTX0BA2jF8agxAiEAyVHzDWQPWx/s
  gt+ABErqN+ZUWS016DD34QUVGyp9nAkCIQC39JpSDcd7gx1YA8jNXCT9N/8mg6+t
  PO84g2d2sPdjEQIgXwWMF/TzfopJ4tfFH8GQXYQcqd66A/cg+Jeih6j9kqkCIGD4
  hBAO0haqnqeSO65Mm1IjY/6Z77pKxzJAGys5XeXk
  -----END RSA PRIVATE KEY-----
  </code></pre>
  
  <p>And its equivalent pub key</p>
  
  <pre><code>openssl rsa -in key -pubout &gt; key.pub
  
  -----BEGIN PUBLIC KEY-----
  MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALOUBygyX11BsDoEIKoZzn2/HAXPorNR
  /X8wCDaBlcPtOHxKAZFkVra1+Pem1urtSlnEqc07DwAP6v0GEGHpxbkCAwEAAQ==
  -----END PUBLIC KEY-----
  </code></pre>
  
  <p>The above plaintext data encrypted with the given public key using the node.js rsa library:</p>
  
  <pre><code>nbp1tBlcev9PvD3xDmEQkcBd8ewNxW8Xm7oZWVcsFika3dU/H3VFoxdeH75DPy4/xLvN1Gxqfb/bXTnfKZLZBYV1q4XfmR4p3ji41MAybpMiEl5h4fSFYacg5SiQ/KxxmQr0SLs4rttwcbaGBLG6rcIU+6SSYBYu1GhC+XBlBG94zbqFV9ZvohEbnlnqDDW1Kg9hGT9/vBBtiLEQnTiKDwztIgY3DhqadsVW0g37PFFwuQKXtHw/lQqrRhc+Pb03g+Oq8nIpX8eaurL8lo3lZNkhlY4NfFCYwP7v12MYwSrMeWPMe20LDDQ6NXbJnrsLGl5x08aYn7liS5qsYdtqRpYv+JbJc3EoXIZEyHv17gU1R0OmLsSd/Teln9VAvM+jt4jwQjlvE1WF8g9Qc/WNo28RR4KaNOvUpLDwfuc3gTgkG90ac8EchmKB3LAgU47kQComyphuPAI/G4phqeXOeYnbBrB1aqwxAkAwOIvanGjCY6FXlV8Cve1jao0ejQ0EFE1180yjhltgh5U2EErQLDd5S4y5YLbLz4xIiKo3k06Yktk4dSJsBalHPxi7Z+kofjT3KdeHyGMynodGzOmH5CaAAS2enZpp2VytcawlDu84EvrPYIRPWah9cA6dtxARx6us8EytrNIDv7UVGXw/cQEPR1nZamz6HROqT4Fpwfc=
  </code></pre>