title: Manually create JWK from (RSA) PublicKey
body: <p>I need to convert a RSA PublicKey into a valid JWK. Especially the values &quot;n&quot; an &quot;e&quot; of the JWK are the ones I'm struggling with. The encoding does not seem to be correct, when looking at example JWK's at <a href="https://www.rfc-editor.org/rfc/rfc7517#page-25" rel="nofollow noreferrer">https://www.rfc-editor.org/rfc/rfc7517#page-25</a></p>
  <p>Currently my code looks basically like this:</p>
  <pre><code>private Map&lt;String, Object&gt; generateJWK(PublicKey publicKey){
  
      RSAPublicKey rsa = (RSAPublicKey) publicKey;
  
      Map&lt;String, Object&gt; values = new HashMap&lt;&gt;();
  
      values.put(&quot;kty&quot;, rsa.getAlgorithm()); // getAlgorithm() returns kty not algorithm
      values.put(&quot;kid&quot;, &quot;someuniqueid&quot;);
      values.put(&quot;n&quot;, Base64.encode(rsa.getModulus().toString()));
      values.put(&quot;e&quot;, Base64.encode(rsa.getPublicExponent().toString()));
      values.put(&quot;alg&quot;, &quot;RS256&quot;);
      values.put(&quot;use&quot;, &quot;sig&quot;);
  
      return values;
  }
  </code></pre>
  <p>The output however does not seem to be correctly encoded or something, <code>e</code> for example looks like this: <code>NjU1Mzc=</code></p>
  <p><code>n</code> does not include special characters like <code>-</code>, <code>_</code> and <code>+</code> . :</p>
  <pre><code>jMzk1MNT0xTk2NED1xzzgNyQ00IykADzMAM0c0wz0M0MONj2z5TgNzI3yAM0OONYzzMjzwNzDxgAzxDxzMMAjTwNNDYMINMgNQDOEAkIM2jMQzkjUTDUYONNg1A00Tw1Nx4YEzAzjUT1MTNMjDjMM1MNNAjyTMIzxNADDINQANwT5yTDEMjEzNz2z2gOgjDDDNyNDjTzz43ETOYMI35gDjE00MYYM2DzDjDgww53Mwz0ME1NMgOM3MIzYTzMwzOMIQU5MjOzUjMNQNNg50U5NIDNzw2DMMOggNcQQM21TI5NMzDTN5Mj123O33MNNMkyNTNONxMM5wMMc04jTgAUE3MM1zMg4NNMT4MNDMM5yTO2j4jNDEMy1yNANNAzOIEUDzNwzExwTIkNjUjkN54Uz0DT5x0zM51k2MxYkx0zMNzxMkDUDTTQN3gAYODATQDDwMDMjMMcONjxMNTYMT5kgxNkMjNMQU0jzMEwIIMzTzUD4MgYDkDNzcAzN0TN4yNTz11DMxDUjDM2MyDMy4DEINMwT22QxjNNEzNDATy1OM1NNDxYgz5TxDkj3gQ32kIwNNkDO3xczDAENcTMNO0MOjTDwE3g11wNUcgNTwQk30kjjNNzTz4jTj4OOjQNYzMzcMjTQMkyzNNNUQOTOMMkMMMNzwNxDOEkg4xADIT4DNxMz2TENT4yN4z2I2zjyMU3DTOEQN4MIQjNDMU5Y11QkccwMNI0kNzyNjMMN4NMTTNMzMwxMjjDzgAANO1zwjYIEUjM1ADgDNjxTITMNNkIYxzyzzEEDMzDzNjzM4NjNNjc3ITTD0T5jzN=
  </code></pre>
  <p>Am I assuming right that both values n and e are not properly encoded?
  How should I convert the PublicKey to JWK? (Can not use 3rd party libraries)</p>