簡體   English   中英

如何驗證 Java 中的 PEM 格式證書

[英]How to verify PEM format certificate in Java

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

您可以使用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不返回任何內容:相反,如果失敗,它們會拋出異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM