Generate X509Certificate from byte[]?

前端 未结 3 1865
庸人自扰
庸人自扰 2021-01-31 07:30

Is there a possibility to generate an java.security.cert.X509Certificate from an byte[]?

3条回答
  •  不知归路
    2021-01-31 07:53

    Sure.

    The certificate objects can be created by an instance of CertificateFactory - in particular, one configured to create X509 certificates. This can be created like so:

    CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
    

    Then you need to pass it an InputStream containing the bytes of the certificate. This can be achieved by wrapping your byte array in a ByteArrayInputStream:

    InputStream in = new ByteArrayInputStream(bytes);
    X509Certificate cert = (X509Certificate)certFactory.generateCertificate(in);
    

提交回复
热议问题