Exporting a Certificate to a File

See also Importing a Certificate from a File.
// This method writes a certificate to a file. If binary is false, the
// certificate is base64 encoded.
public static void export(java.security.cert.Certificate cert, File file, boolean binary) {
    try {
        // Get the encoded form which is suitable for exporting
        byte[] buf = cert.getEncoded();

        FileOutputStream os = new FileOutputStream(file);
        if (binary) {
            // Write in binary form
            os.write(buf);
        } else {
            // Write in text form
            Writer wr = new OutputStreamWriter(os, Charset.forName("UTF-8"));
            wr.write("-----BEGIN CERTIFICATE-----\n");
            wr.write(new sun.misc.BASE64Encoder().encode(buf));
            wr.write("\n-----END CERTIFICATE-----\n");
            wr.flush();
        }
        os.close();
    } catch (CertificateEncodingException e) {
    } catch (IOException e) {
    }
}
If the certificate is in the key store, it can exported using keytool:
// Export in binary
> keytool -storepass my-keystore-password -alias myalias -export -file outfilename.cer

// Export in text format
> keytool -storepass my-keystore-password -alias myalias -export -rfc -file outfilename.cer
Here's an example of the text form of an exported certificate:
-----BEGIN CERTIFICATE-----
MIIC6TCCAqcCBDxgu/IwCwYHKoZIzjgEAwUAMFoxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTES
MBAGA1UEBxMJUGFsbyBBbHRvMQowCAYDVQQKEwFJMQswCQYDVQQLEwJNZTERMA8GA1UEAxMIUGF0
IENoYW4wHhcNMDIwMjA2MDUxNTMwWhcNMDIwNTA3MDUxNTMwWjBaMQswCQYDVQQGEwJVUzELMAkG
A1UECBMCQ0ExEjAQBgNVBAcTCVBhbG8gQWx0bzEKMAgGA1UEChMBSTELMAkGA1UECxMCTWUxETAP
BgNVBAMTCFBhdCBDaGFuMIIBuDCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2
EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdrmVClpJ+f6AR7
ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXzrith1yrv8iIDGZ3RSAHHAhUA
l2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+GghdabPd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdR
WVeOutRZT+ZxBxCBgLRJFnEj6EwoFhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx
+2J6ASQ7zKTxvqhRkImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoDgYUAAoGBAPyx9uQ1PKBYO/2G
RPzbW4y6pphNRmObJQWbjY/ERuCQwLRrpREh9sgMnptZjRzLVpWdzxNa9bFMFXAYMgoTUIgAZ9yN
WPjp/JiFfzdIq3CY0CEey42M3mbD3pWsF9x4SSsJTpDobX/pm5XgtkhZXBZYtBk813Xv2LxyZ3OI
W1JnMAsGByqGSM44BAMFAAMvADAsAhQ5wayd5cpEo/vHmF7G5gVQ9cMKKAIUMfk2ZYxNdhe6oNmH
nR0AhnEHILE=
-----END CERTIFICATE-----

Comments

19 Mar 2010 - 12:34am by Anonymous (not verified)

hey it's good website for getting trail versions of certificates

24 Aug 2011 - 3:43am by Dipesh Srivastava (not verified)

Thanks

4 Sep 2011 - 12:19pm by personal loans (not verified)

I received 1 st mortgage loans when I was very young and that aided my business very much. However, I need the commercial loan over again.

4 Oct 2011 - 8:24pm by Cheap Moncler Sale (not verified)

While it’s accurate that a cheaper backpack can save you some banknote up front, but a beautiful and affected Ugg boots will accord you added amount for your money in the continued run. A archetypal backpack or purse from Ugg will absolutely endure you for years.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.