PKCS10CertificationRequest csr =...;
PublicKey pk = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(csr.getSubjectPublicKeyInfo().toASN1Primitive().getEncoded()));
RFC 2986 - PKCS #10:认证请求语法
CertificationRequestInfo ::= SEQUENCE {
version INTEGER { v1(0) } (v1,...),
主题名称,
subjectPKInfo SubjectPublicKeyInfo{{ PKInfoAlgorithms }},
属性 [0] 属性{{ CRIAttributes }}
}
SubjectPublicKeyInfo { ALGORITHM : IOSet} ::= SEQUENCE {
算法 AlgorithmIdentifier {{IOSet}},
subjectPublicKey BIT STRING
}
然后就可以看到java.security.spec.X509EncodedKeySpec的文档了
SubjectPublicKeyInfo ::= SEQUENCE {
算法 AlgorithmIdentifier,
subjectPublicKey BIT STRING }
所以你会知道这个公钥的编码是 X.509。然后将其更改为 X509EncodedKeySpec 并通过 keyFactory 生成公钥