title: error:0409A06E:rsa routines data too large for key size
body: <p>I'm generating a SAML response and it needs to be encrypted and signed with public and private keys. I generated <code>private.pem</code> and <code>public.pem</code> in the terminal with the commands</p>
  
  <pre><code>openssl genrsa -out private.pem 2048
  openssl rsa -in ./private.pem -pubout -out public.pem
  </code></pre>
  
  <p>Then in nodeJS. </p>
  
  <pre><code>encrypt: function(message) {
      return new Promise(function (resolve, reject) {
          var publicKey = require("fs").readFileSync(__dirname + "/public.pem", "utf8");
          var encrypted = require("crypto").publicEncrypt(publicKey, new Buffer(message));
          resolve(encrypted.toString("base64"));
      });
  },
  </code></pre>
  
  <p>Once I call the message <code>encrypt(xml)</code>, I get the following error</p>
  
  <pre><code>{
    library: 'rsa routines',
    function: 'RSA_padding_add_PKCS1_OAEP_mgf1',
    reason: 'data too large for key size',
    code: 'ERR_OSSL_RSA_DATA_TOO_LARGE_FOR_KEY_SIZE'
  }
  </code></pre>
  
  <p><strong>Objective</strong>:</p>
  
  <p>I've to sign the message as per the demo here <a href="https://www.samltool.com/sign_response.php" rel="nofollow noreferrer">samltools.com</a> (<em>Mode: SignMessage</em>), my SAML message looks like <a href="https://www.samltool.com/generic_sso_res.php" rel="nofollow noreferrer">this</a>. (<em>see SAML Response section</em>). </p>
  
  <ol>
  <li>Sign the message</li>
  <li>Base64Encode the message</li>
  </ol>