如何在 Java 中提取 X509 证书字段 [英] How to extract X509 Certificate fields in Java
问题描述
我目前正在开发一个应用程序,该应用程序将处理 X509 证书的某些字段,但我似乎无法弄清楚如何提取证书的某些部分以进行调试.到目前为止,我只能使用以下代码弄清楚如何从基于 java.security.cert.Certificate 的 Javadoc 的文件中读取证书:
I am currently working on an application that will process certain fields of a X509 Certificate, and I cannot seem to figure out how to extract certain parts of the certificate for debugging purposes. So far I have only been able to figure out how to read a certificate from a file based on the Javadoc for java.security.cert.Certificate, using this code:
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
while (bis.available() > 0) {
Certificate cert = cf.generateCertificate(bis);
System.out.println(cert.toString());
}
假设没有抛出异常,并且该证书是有效证书,我该怎么做?
Assuming that no exceptions are thrown, and that cert is a valid certificate, how would I do this?
旁注我在这个项目中使用了 Bouncy Castle
Sidenote I am using Bouncy Castle in this project
推荐答案
Cast it to an X509Certificate
:
Cast it to an X509Certificate
:
X509Certificate cert = (X509Certificate) cf.generateCertificate(bis);
System.out.println(cert.getSubjectDN());
这篇关于如何在 Java 中提取 X509 证书字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!