0

I made a terrible mistake saving a list of X509Certificates using the "toString()" method. The library used is "javax.net.ssl.java.security.cert.X509Certificate". I'd like to recover them and save them properly using either a PEM or DER format but all I can find on StackOverflow is about reading proper DER/PEM certs.

The way they look currently on the files is:

http://www.heypasteit.com/clip/18XK

Any help will be much appreciated.

UPDATE FOR REFERENCE:

That's the way I'm storing it for Android:

protected static String convertToPem(X509Certificate cert) throws CertificateEncodingException {

    String cert_begin = "-----BEGIN CERTIFICATE-----\n";
    String end_cert = "-----END CERTIFICATE-----";

    String pemCert = "";

    byte[] derCert;
    try {
        derCert = cert.getEncoded();
        String pemCertPre = Base64.encodeToString(derCert, Base64.DEFAULT);
        pemCert = cert_begin + pemCertPre + end_cert;
    } catch (java.security.cert.CertificateEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return pemCert;

}

Narseo
  • 214
  • 1
  • 4
  • 11

1 Answers1

1

You can't. It's too late. There's not enough information in the toString() method's output.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks. I need to upload the certificate to a server using HTTP. What would be the method that you recommend to convert the certificate using getEncoded() from a byte array to a String? – Narseo Mar 30 '14 at 00:03
  • You'll probably want to send it in RFC format. You can get that via the key tool. – user207421 Mar 30 '14 at 00:11