简体   繁体   English

Java - 如何将X509Certificate链数据导出到Base64编码的证书文件?

[英]Java - How to export X509Certificate chain data to Base64 encoded certificate file?

When I receive X509Certificate chain on SSL Handshake in my socket connection, I have to export X509Certificate chain to a base64 encoded .cer file. 当我在套接字连接中的SSL握手上收到X509Certificate链时,我必须将X509Certificate链导出到base64编码的.cer文件。

I tried with below code. 我试过下面的代码。 But the file content is not as same with original certificate. 但文件内容与原始证书不同。

private static final String BEGIN_CERT = "-----BEGIN CERTIFICATE-----";

private static final String END_CERT = "-----END CERTIFICATE-----";

X509Certificate[] x509Certificates;


   --------------
   --------------


  String certContent="";

  for(int i=0;i<x509Certificates.length;i++)   
  {  

         certContent += Base64.encode(x509Certificates[i].getEncoded());

  } 



BufferedWriter writer = new BufferedWriter(new FileWriter("mycert.cer"));

writer.write(BEGIN_CERT);

writer.newLine();

writer.write(certContent);

writer.newLine();

writer.write(END_CERT);

writer.close();

Any suggestion/correction in above code? 上述代码中的任何建议/更正?

I believe your code is concatenating the certificates together and then adding the BEGIN and END tags around the whole thing. 我相信您的代码将证书连接在一起,然后在整个事物周围添加BEGIN和END标记。

Instead try placing the BEGIN/END tags around EACH certificate within the same file. 而是尝试将BEGIN / END标记放在同一文件中的EACH证书周围。

For example 例如

-----BEGIN CERTIFICATE-----
<Base64 cert1 data>
-----END CERTIFICATE-----

-----BEGIN CERTIFICATE-----
<Base64 cert2 data>
-----END CERTIFICATE-----

-----BEGIN CERTIFICATE-----
<Base64 certN data>
-----END CERTIFICATE-----

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM