从byte []生成X509Certificate?

时间:2010-08-02 15:20:52

标签: java x509certificate

是否有可能从byte []生成java.security.cert.X509Certificate?

3 个答案:

答案 0 :(得分:70)

不确定

证书对象可以由CertificateFactory的实例创建 - 特别是,一个配置为创建X509证书的实例。这可以这样创建:

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

然后你需要传递一个包含证书字节的InputStream。这可以通过将字节数组包装在ByteArrayInputStream

中来实现
InputStream in = new ByteArrayInputStream(bytes);
X509Certificate cert = (X509Certificate)certFactory.generateCertificate(in);

答案 1 :(得分:0)

您可以执行以下操作:

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));

答案 2 :(得分:-1)

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();
}