I have a requirement to read the public key using java and analyze it, check its authenticity, exponent, module or its validity or not. I tried the code below and had problems. Can you help me find a solution to this problem?
public static void getPublicKey(String key) throws Exception { key = key.replaceAll("-----BEGIN SSH2 PUBLIC KEY-----", ""); key = key.replaceAll("-----END SSH2 PUBLIC KEY-----", ""); KeyFactory kFactory = KeyFactory.getInstance("RSA", new BouncyCastleProvider()); byte pub_llave[] = new BASE64Decoder().decodeBuffer( key ) ; X509EncodedKeySpec spec = new X509EncodedKeySpec(pub_llave); PublicKey pubkey = (PublicKey) kFactory.generatePublic(spec); }
And here is the exception:
java.lang.IllegalArgumentException: unknown object in getInstance: org.bouncycastle.asn1.DERApplicationSpecific at org.bouncycastle.asn1.ASN1Sequence.getInstance(Unknown Source) at org.bouncycastle.asn1.ASN1Sequence.getInstance(Unknown Source) at org.bouncycastle.asn1.x509.SubjectPublicKeyInfo.getInstance(Unknown Source)
source share