Search code examples
javaandroidbouncycastlespongycastle

Export RSA public key to PEM String using java


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:

    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();

Now as you can probably tell I'm not sure how to construct the PemObject or if there is an easier way to do this.

When using Bouncy Case I used to do this like this

    StringWriter writer = new StringWriter();
    PEMWriter pemWriter = new PEMWriter(writer);
    pemWriter.writeObject(keyPair.getPublic());
    pemWriter.flush();
    pemWriter.close();
    return writer.toString();

But for some reason the PEMWriter class does not exist in Spongy Castle


Solution

  • Ok So this is probably not the smartest way (or maybe it is?) to do it, but after checking out the sources for PEMWriter this class basically does this under the hood:

    1. when calling writeObject it creates an instance of MiscPEMGenerator
    2. MiscPEMGenerator then creates the PemObject by checking the type of the constructor's argument, the following is an excerpt from MiscPEMGenerator's source:

      private PemObject createPemObject(Object o){
        ...
        else if (o instanceof PublicKey)
        {
            type = "PUBLIC KEY";
      
            encoding = ((PublicKey)o).getEncoded();
        }
        ...
        return new PemObject(type, encoding);
      }
      

    So as can be seen from the MiscPEMGenerator code the only thing I had to change was the type parameter from "RSA PUBLIC KEY" to just "PUBLIC KEY". Here is the final code.

    PublicKey publicKey = keyPair.getPublic();
    StringWriter writer = new StringWriter();
    PemWriter pemWriter = new PemWriter(writer);
    pemWriter.writeObject(new PemObject("PUBLIC KEY", publicKey.getEncoded()));
    pemWriter.flush();
    pemWriter.close();
    return writer.toString();