Write x509 certificate into PEM formatted string in java?

后端 未结 10 2314
天涯浪人
天涯浪人 2020-12-07 15:48

Is there some high level way to write an X509Certificate into a PEM formatted string? Currently I\'m doing x509cert.encode() to write it into a DER formatted string, then ba

10条回答
  •  佛祖请我去吃肉
    2020-12-07 16:23

    Yet another alternative for encoding using Guava's BaseEncoding:

    import com.google.common.io.BaseEncoding;
    
    public static final String LINE_SEPARATOR = System.getProperty("line.separator");
    public static final int LINE_LENGTH = 64;
    

    And then:

    String encodedCertText = BaseEncoding.base64()
                                         .withSeparator(LINE_SEPARATOR, LINE_LENGTH)
                                         .encode(cert.getEncoded());
    

提交回复
热议问题