如何在Java中验证PEM格式的证书

发布于 2024-11-15 05:49:15 字数 229 浏览 15 评论 0原文

我有 PEM 格式文件,如何验证 Java 中的签名,正如我所遵循的 http://download.oracle.com/javase/tutorial/security/apisign/versig.html 但发现Java不支持PEM

I have PEM format file, How can verify the signature in Java, as I followed http://download.oracle.com/javase/tutorial/security/apisign/versig.html but found that Java doesnt support PEM

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵情客 2024-11-22 05:49:15

您可以使用 BouncyCastlePEMReader 读取 PEM 文件中的证书。如果内容是 X.509 证书,您应该获取 X509Certificate 的实例,并根据需要从那里验证它。

:代码应如下所示(未尝试):(

// The key with which you want to verify the cert.
// This is probably a CA certificate's public key.
PublicKey publicKey = ...;

PEMReader reader = new PEMReader(new FileReader("/path/to/file.pem"));
Object pemObject = reader.readObject();
if (pemObject instanceof X509Certificate) {
    X509Certificate cert = (X509Certificate)pemObject;
    cert.checkValidity(); // to check it's valid in time
    cert.verify(publicKey); // verify the sig. using the issuer's public key
}

当然,与任何 I/O 操作一样,您可能需要使用 try/finally 关闭阅读器。)

编辑 checkValidityverify 不会返回任何内容:相反,如果失败,它们会抛出异常。

You can read a certificate in a PEM file using BouncyCastle's PEMReader. If the content is an X.509 certificate, you should get an instance of X509Certificate and verify it as you want from there.

EDIT: Here is what the code should look like (not tried):

// The key with which you want to verify the cert.
// This is probably a CA certificate's public key.
PublicKey publicKey = ...;

PEMReader reader = new PEMReader(new FileReader("/path/to/file.pem"));
Object pemObject = reader.readObject();
if (pemObject instanceof X509Certificate) {
    X509Certificate cert = (X509Certificate)pemObject;
    cert.checkValidity(); // to check it's valid in time
    cert.verify(publicKey); // verify the sig. using the issuer's public key
}

(Of course, as with any I/O operations, you'll need to close the reader perhaps with try/finally.)

Note that checkValidity and verify don't return anything: instead, they throw exceptions if when they fail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文