如何从Java中读取X.509的扩展?

时间:2013-01-01 19:59:27

标签: java x509certificate x509 verify

我已经使用X.509创建了openssl证书,证书具有我需要能够在Java代码中读取的自定义扩展。我使用以下代码创建了证书:

openssl req -new -x509 -key myCA.key -out mtCA.csr -days 1000 -config openssl.cfg

现在,在Java代码中读取证书中扩展名值的最简单方法是什么?或者更一般的是解析x509证书的最简单方法是什么?

1 个答案:

答案 0 :(得分:0)

使用java.security.cert.CertificateFactory和java.security.cert.X509Certificate

private void read(File file) throws Exception {
    InputStream in = new FileInputStream(file);
    CertificateFactory factory = CertificateFactory.getInstance("X.509");
    X509Certificate certificate = (X509Certificate) factory.generateCertificate(in);
    // read X509Certificate
}