Export RSA public key to PEM String using java

走远了吗. 提交于 2019-11-30 21:35:06

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

The reason you might be running into issues is because Android uses Bouncy Castle internally as JCA provider. But the version that is included varies over the various Android versions.

You might be interested in the Spongy Castle project, which "simply" repacks Bouncy Castle in a different package so that you can include your own libraries with your Android application.

Using the latest version (1.51.0.0) of Spongy Castle, the following works as intended (on my workstation, no device at hand to test on a device):

import java.security.PublicKey;
import org.spongycastle.openssl.jcajce.JcaPEMWriter;

class PEMConverter {

    public static String toPEM(PublicKey pubKey) {
        StringWriter sw = new StringWriter();
        JcaPEMWriter pemWriter = new JcaPEMWriter(sw);
        pemWriter.writeObject(pubKey);
        pemWriter.close();
        return sw.toString();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!