我有一种使用 bouncycastle 库从给定 PEM 格式文件中提取 X.509 证书的方法。
进口:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.openssl.PEMParser;
Run Code Online (Sandbox Code Playgroud)
方法:
/**
* Reads an X509 certificate from a PEM file.
*
* @param certificateFile The PEM file.
* @return the X509 certificate, or null.
* @throws IOException if reading the file fails
* @throws CertificateException if parsing the certificate fails
*/
public static X509Certificate readCertificatePEMFile(File certificateFile) throws IOException, CertificateException {
if …Run Code Online (Sandbox Code Playgroud)