从字符串数据中获取X509证书到期日期

时间:2012-07-18 16:54:34

标签: java jsp

我正在尝试从标记

下的xml文件中获取x509证书的到期日期
<ds:X509Certificate> MBaAFI7JTi5oRslwv2B3MmERGbPKKUsSMFwGA1UdIARVMFMwUQYKKwYBBAEJFQEBADBDMEEGCCsG
AQUFBwIBFjVodHRwOi8vd3d3LmNpc2NvLmNvbS9zZWN1cml0eS9wa2kvcG9saWNpZXMvaW5kZXgu
aHRtbDATBgNVHSUEDDAKBggrBgEFBQcDATANBgkqhkiG9w0BAQUFAAOCAQEAs2DJZwgVLmZqQS2N
3Yp4iGYhZBG6vZSDaiaAg6u1wgrfq/byBd7H3Ei+V9I7MZ1aCh4s3kl6FJ1CoRhMQcM1gbxpCbPP
KAfrMv/5/MH8erMnole7m/MqVHETX1UbLEyagOioqK3cs8MzMKxC+Fdku6Bm+i/6xhUsc6XbrVJl
IHsVBB9+9ZnpjAjC9jj21r04N51cyfp6C37999TaybNEtCJlG0XTHfLbcyX65E9ndVaRd6GYu/d0
xW0MT5bKhA36KhpUROAbPS7rXKKkfCYnMkmImE7Hfvm3vRSahX+ehY1Tx9qJXFv3PYGzbyDbvpjV
2cSUgP/bhGATKSOZuhohrw==  </ds:X509Certificate

&GT;

我有将其读取为字符串的代码

NodeList nodelist = element.getElementsByTagName("ds:X509Certificate");
Element element1 = (Element) nodelist.item(0);
NodeList certificateItem = element1.getChildNodes();
String certificateValue = "---- BEGIN CERTIFICATE -------- " + "\n" +
"certificateItem.item(0).getNodeValue()" + "\n" + "--- END CERTIFICATE";                            
System.out.print("Certificate data is : " + certificateValue);
InputStream certinputstream = new ByteArrayInputStream(certificateValue.getBytes());
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate)cf.generateCertificate(certinputstream);
System.out.println("Expiry Date : " + cert.getNotAfter());

但是我收到以下错误 java.security.cert.CertificateParsingException:无效的DER编码证书数据     在sun.security.x509.X509CertImpl.parse(未知来源)     at sun.security.x509.X509CertImpl。(未知来源) 有任何想法吗 ?感谢

1 个答案:

答案 0 :(得分:0)

目前尚不清楚这是所有是否错误,但您没有使用CertificateFactory文档中指定的精确格式:

  

如果证书是以Base64编码提供的,那么它必须在----- BEGIN CERTIFICATE -----的开头绑定,并且最后必须以----- END CERTIFICATE--为界。 ---

文本周围有空格,破折号错误,END CERTIFICATE后没有破折号。

您可能还想修剪节点值,因为它当前在数据后面有几个空格。