2

I receive a public key from my REST API which is encoded in PEM. Now, I need to change the encoding to DER in order to store it as a PublicKey object. However, unfortunately, I get the following error:

java.lang.IllegalArgumentException: unknown object in getInstance: org.spongycastle.asn1.ASN1Integer

And the error is pointing to this line:

RSAKeyParameters rsa = (RSAKeyParameters) PublicKeyFactory.createKey(pemReader.readPemObject().getContent());

The public key looks like this:

-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEA04XyJ5K4sQCtqapk98yEAR/ECaVC85JYPsqC09GiZboNdSSxQXj3
UNb53Po8iNX24T4elyjjzqQpVcyi+eaFp9Fggg2ZDyK9Re1wTucs0APDQdsGe1Q5
KImT/SBycI7v1RwSgjQ4I6npMg/0lZY8bnw4Q1AaTIII0KFBHmIYBD1oeCSdVPED
JWQWTSXtStQj83Vyj1uSLEEzXLpVYW4fq8e24tH2D/1j6eIBnBw6YpkWE6T9pZlE
wIs8YoeQWt5+lPWI28PST8VKqXsxH6JVzu5Mj6jLw8WTZxyKvNaGgO4B3J/ze/58
zW0LtlhsKMEq48QdLgPJZ+tfac2EhKANrQIDAQAB
-----END RSA PUBLIC KEY-----

Here is my code:

public void setPublicKey(String publicKey) {
    try {
        PemReader pemReader = new PemReader(new StringReader(publicKey));
        RSAKeyParameters rsa = (RSAKeyParameters) PublicKeyFactory.createKey(pemReader.readPemObject().getContent());
        pemReader.close();
        RSAPublicKeySpec rsaSpec = new RSAPublicKeySpec(rsa.getModulus(), rsa.getExponent());
        KeyFactory kf = KeyFactory.getInstance("RSA/ECB/PKCS1Padding");
        this.publicKey = kf.generatePublic(rsaSpec);
    } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException ex) {
        ex.printStackTrace();
        throw new IllegalStateException("Cannot set public key.");
    }
}
user3475602
  • 1,217
  • 2
  • 21
  • 43
  • where exactly you get NPE? in `new PemReader(new StringReader(publicKey));` statement? Can you provide a sample of public key you trying to convert? – user1516873 Dec 16 '15 at 15:06
  • Thank you for your help. Well, I received the NPE, because I removed `-----BEGIN RSA PUBLIC KEY-----` and `-----END RSA PUBLIC KEY-----` from the key manually on the backend. Now, I deleted the substring operation and I get this error: `java.lang.IllegalArgumentException: unknown object in getInstance: org.spongycastle.asn1.ASN1Integer`. – user3475602 Dec 16 '15 at 15:41

1 Answers1

3

Java really wants to see an encoded SubjectPublicKeyInfo object, but you have a simpler PKCS#1 RSAPublicKey object.

The easiest way I know of is to use the PEMParser class from bouncycastle PKIX library -- there should be an equivalent library from Spongycastle. Use this class in lieu of the PemReader class you are currently using, as in:

        PEMParser pemParser = new PEMParser(new StringReader(publicKey));
        SubjectPublicKeyInfo spki = (SubjectPublicKeyInfo) pemParser.readObject();
        pemParser.close();
        byte [] spkiEncoded = spki.getEncoded();
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(spkiEncoded);
        
        KeyFactory kf = KeyFactory.getInstance("RSA");
        this.publicKey = kf.generatePublic(keySpec);
Community
  • 1
  • 1
President James K. Polk
  • 40,516
  • 21
  • 95
  • 125