如何从pem文件加载公共证书?

我试图从下面的文件中提取RES公钥

-----BEGIN CERTIFICATE-----
MIIGwTCCBamgAwIBAgIQDlV4zznmQiVeF45Ipc0k7DANBgkqhkiG9w0BAQUFADBmMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3d3cuZGlnaWNlcnQuY29tMSUwIwYDVQQDExxEaWdpQ2VydCBIaWdoIEFzc3VyYW5jZSBDQS0zMB4XDTEyMTAzMDAwMDAwMFoXDTE1MTEwNDEyMDAwMFowgYIxCzAJBgNVBAYTAlVTMQ4wDAYDVQQIEwVUZXhhczEQMA4GA1UEBxMHSG91c3RvbjEpMCcGA1UEChMgVmFsZXJ1cyBDb21wcmVzc2lvbiBTZXJ2aWNlcywgTFAxCzAJBgNVBAsTAklUMRkwFwYDVQQDDBAqLnZhbGVydXMtY28uY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1GR2NKV9GwVHBtpvgBUdVVbd6qeh6aKOS/r5TIKFd3vFBGjC7cWYwF26F0YFvrAP262Yu+oDRTeuSKwyHmegD7aTSOyCTOva69WcnKYRmNfHsnnGRa5z4v9EKc1RbNcwIrDUz8zcdHdP6AO8JJgLreWyBl15WXdxAr3yNbwoyJTbWk2ToC64LASP+8SQQTRszg762FIbhZ8xda8KKGAyC29/FOcLIttoBANT4hEwvcRLKOxAA8tg322Dla1XU2gnxWP2dSuLEflGRcEovPjGqxCzuGe0aN8Lg7aKwgCR1OYXmGiKCNHupHkN7A+QrD8zrxKUFd1UiyLcIovYhadcdQIDAQABo4IDTDCCA0gwHwYDVR0jBBgwFoAUUOpzidsp+xCPnuUBINTeeZlIg/cwHQYDVR0OBBYEFKKX1d9m6kHUjxQ1OpzXgNRbNGR4MCsGA1UdEQQkMCKCECoudmFsZXJ1cy1jby5jb22CDnZhbGVydXMtY28uY29tMA4GA1UdDwEB/wQEAwIFoDATBgNVHSUEDDAKBggrBgEFBQcDATBhB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qbH6P32/2yyuh4hcNL4Vz8G0MTzwVUjz8WVmUBHAQSpS0T9oDKkwvmrkPGJFVuBxCRDKYb/23O8EKKzSTiO37VbCaeFUrTuWc8tGP8XDqRdj2yefiVqcNp4xr2tq9ZhJcISWODqO4fzt6vPOwgdnY3fbPLeH2tZoZTSCPURAadoNOAIC6fCLFlHjLuRGkxWIHMX3QnrrVD8pC7FnDO09q/aADew=
-----END CERTIFICATE-----

这是我做的代码。

public static PublicKey loadPublicKeyFromFile(File publicKeyFile) throws Exception {

    FileReader file = new FileReader(publicKeyFile);
    PemReader reader = new PemReader(file);
    X509EncodedKeySpec caKeySpec = new X509EncodedKeySpec(reader.readPemObject().getContent());
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PublicKey caKey = kf.generatePublic(caKeySpec);
    return caKey;
}

但它会抛出

java.security.InvalidKeyException: IOException: ObjectIdentifier() -- data not a object ID

从文件中提取RES公钥的适当方法是什么。


答案 1

X.509 证书和 X509 编码的KeySpec 是完全不同的结构,尝试将证书解析为密钥将不起作用。Java的X509EncodedKeySpec实际上是来自X.509的SubicPublicKeyInfo或等效的,更方便的PKIX也从Key链接,这只是证书的一小部分。

您需要做的是读取并解析证书,然后从证书中提取公钥。标准 SunJCE 可以这样做(并且可以读取 PEM 或 DER 来引导),如下所示:CertificateFactory

CertificateFactory fact = CertificateFactory.getInstance("X.509");
FileInputStream is = new FileInputStream (args[0]);
X509Certificate cer = (X509Certificate) fact.generateCertificate(is);
PublicKey key = cer.getPublicKey();
is.close();
// add error handling as appropriate, try-with-resources is often good

如果你有 BouncyCastle,你可以以同样的方式使用它的提供程序(只需添加第二个参数或设置默认的提供程序列表顺序),或者你可以使用 -- 它有效地做同样的事情,通过 ..getInstancePEMParserJcaX509CertificateConverterCertificateFactory


答案 2

使用 Sun JVM,也可以使用

import java.security.cert.X509Certificate;
import sun.security.x509.X509CertImpl;

InputStream is = ...
X509Certificate crt = new X509CertImpl(is);

,尽管我更喜欢接受的答案作为与JVM实现无关的答案。

在引擎盖下,在 Sun JVM 中(更确切地说,)确实实例化 ,但这两种方法之间存在非常细微的区别:通过输入流的二进制内容缓存 X509 证书实例,该实例可以由 检索。CertificateFactoryX509FactoryX509CertImplCertificateFactorycer.getEncoded()

缓存可以通过以下方式清除

fact.generateCertificate(null);