Generate X509Certificate from byte[]?

前端 未结 3 1866
庸人自扰
庸人自扰 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);
    
    0 讨论(0)
  • 2021-01-31 07:57

    You can do something like:

    X509Certificate certificate = signature.getKeyInfo().getX509Datas().get(0).getX509Certificates().get(0);
    
    String lexicalXSDBase64Binary = certificate.getValue();
    byte[] decoded = DatatypeConverter.parseBase64Binary(lexicalXSDBase64Binary);
    
    
    CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
    Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(decoded));
    
    0 讨论(0)
  • 2021-01-31 08:07
    InputStream stream = null;
    byte[] bencoded = javax.xml.bind.DatatypeConverter.parseBase64Binary(x509CertificateStr);
    
    try {
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        cert = (X509Certificate) certFactory.generateCertificate(stream);
    
    } catch (java.security.cert.CertificateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    0 讨论(0)
提交回复
热议问题