简体   繁体   English

如何序列化/反序列化X509Certificate

[英]how to serialize/deserialize X509Certificate

I see there is code to read one in here 我看到这里有代码可以读一个

https://docs.oracle.com/javase/8/docs/api/java/security/cert/X509Certificate.html https://docs.oracle.com/javase/8/docs/api/java/security/cert/X509Certificate.html

but there is no code for writing one out. 但没有写出代码。

My preference is to write a String and read a String or else I will have to base64 encode. 我的偏好是编写一个String并读取一个String,否则我将不得不进行base64编码。 Anyways, how to serialize it? 无论如何,如何序列化它?

thanks, Dean 谢谢,院长

Use this to write a certificate as binary 用它来写一个二进制证书

Files.write(
      Paths.get(fileName),
      certificate.getEncoded());

And this code will write the certificate to base64 并且此代码会将证书写入base64

 Files.write(
      Paths.get(fileName),
      Base64.getEncoder().encode(certificate.getEncoded()));

If you need a PEM file, just add -----BEGIN CERTIFICATE----- header and -----END CERTIFICATE----- footer to the base64 file 如果需要PEM文件,只需将-----BEGIN CERTIFICATE-----标头和-----END CERTIFICATE-----页脚添加到base64文件中

Java supports reading certificates encoded in PEM or binary format Java支持读取以PEM或二进制格式编码的证书

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

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