title: Export RSA public key to PEM String using java
body: <p>So I'm using Spongy Castle (Android) to generate a PEM encoded string for an RSA public key that will be uploaded to a server.
  This is what I'm currently doing:</p>
  
  <pre><code>    PublicKey publicKey = keyPair.getPublic();
      StringWriter writer = new StringWriter();
      PemWriter pemWriter = new PemWriter(writer);
      pemWriter.writeObject(new PemObject("RSA PUBLIC KEY", publicKey.getEncoded()));
      pemWriter.flush();
      pemWriter.close();
      return writer.toString();
  </code></pre>
  
  <p>Now as you can probably tell I'm not sure how to construct the <code>PemObject</code> or if there is an easier way to do this.</p>
  
  <p>When using Bouncy Case I used to do this like this</p>
  
  <pre><code>    StringWriter writer = new StringWriter();
      PEMWriter pemWriter = new PEMWriter(writer);
      pemWriter.writeObject(keyPair.getPublic());
      pemWriter.flush();
      pemWriter.close();
      return writer.toString();
  </code></pre>
  
  <p>But for some reason the PEMWriter class does not exist in Spongy Castle</p>