Lee Dat
Lee Dat

Reputation: 165

Problem creating RSA public key from text in Bouncy Castle

I have a problem to create a public key from a public key text. I found the solution from this link Creating RSA Public Key From String. They mentioned Bouncy Castle (lightweight API) as a library to solve the InvalidKeySpecException error when converting a public key string to a RSA public key. But this solution failed with my case. The program throws an exception here

Exception in thread "main" java.lang.IllegalArgumentException: Bad sequence size: 9

Creating RSA Public Key From String

   String publicKeyB64 = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3AQKDhhtcM5A1a8R9/VX" +
            "mrocKGaQlat2/MRFy/Y1fTabYyKkfgaRXyrHiRn+imq3ljEgx/vLRTTPtLt8H79a" +
            "iMU6WJkQwG504NCnDRVB9DZBoAYDtBkjtje7I2Xs3tzvlNwM0bcCmmj/6QE9rHEv" +
            "xhvvXO8M332hINORLNiCF6NvYHrIVSa8EU4F0bnlWpoNi0YhP45uyOOuPpVmsaxp" +
            "MWOycf3nTICKK5BDylnVO7kMcL1utJxOOb1fsotaLuge4fF84DG4cPpLZko3ksB/" +
            "voOLTDv5QRsn++8qRciK4sptlnOs8g2TrXjE/rZlP9QmpUV4a3iQ1WmsqWQVizmw" +
            "PwIDAQAB";

    byte[] decoded = Base64.getDecoder().decode(publicKeyB64);
    org.bouncycastle.asn1.pkcs.RSAPublicKey pkcs1PublicKey = org.bouncycastle.asn1.pkcs.RSAPublicKey.getInstance(decoded);
    BigInteger modulus = pkcs1PublicKey.getModulus();
    BigInteger publicExponent = pkcs1PublicKey.getPublicExponent();
    RSAPublicKeySpec keySpec = new RSAPublicKeySpec(modulus, publicExponent);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PublicKey generatedPublic = kf.generatePublic(keySpec);
    System.out.printf("Modulus: %X%n", modulus);
    System.out.printf("Public exponent: %d ... 17? Why?%n", publicExponent); // 17? OK.
    System.out.printf("See, Java class result: %s, is RSAPublicKey: %b%n", generatedPublic.getClass().getName(), generatedPublic instanceof RSAPublicKey);

So I'm really expecting advice to handle this.

Upvotes: 3

Views: 8828

Answers (2)

dave_thompson_085
dave_thompson_085

Reputation: 38771

The data you have now edited is a public key, but not in PKCS1 format; it is in the more common (and usually more useful) X.509 SubjectPublicKeyInfo format. This difference is explained in the Q you linked. While this format is supported by BouncyCastle, it is also supported directly by Java crypto (JCA) using the (technically imprecise) name X509EncodedKeySpec, so it is much simpler to just do:

byte[] decoded = Base64.getDecoder().decode(publicKeyB64);
KeyFactory kf = KeyFactory.getInstance("RSA");
PublicKey generatedPublic = kf.generatePublic(new X509EncodedKeySpec(decoded));

Upvotes: 4

Gautam
Gautam

Reputation: 1932

If your public key is proper, then you should be able to read it on command line in order to break down the problem. Try using these commands :

$ openssl rsa -inform PEM -pubin -in pub.key -text -noout
$ openssl pkey -inform PEM -pubin -in pub.key -text -noout

Replace "pub.key" with your public key file.

Upvotes: 0

Related Questions