我需要使用 java 读取公钥并对其进行解析,以检查其有效性、指数、模数或是否有效。我尝试了以下代码,但遇到了问题。你能帮我找到解决这个问题的方法吗?
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);
}
这是一个例外:
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)