Write x509 certificate into PEM formatted string in java?

后端 未结 10 2297
天涯浪人
天涯浪人 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:02

    To build on ZZ Coder's idea, but without using the sun.misc classes that aren't guaranteed to be consistent between JRE versions, consider this

    Use Class:

    import javax.xml.bind.DatatypeConverter;
    

    Code:

    try {
        System.out.println("-----BEGIN CERTIFICATE-----");
        System.out.println(DatatypeConverter.printBase64Binary(x509cert.getEncoded()));
        System.out.println("-----END CERTIFICATE-----");
    } catch (CertificateEncodingException e) {
        e.printStackTrace();
    }
    

提交回复
热议问题