Java Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

Java Examples

Quick Start
Java Unicode
Java Certs
Java Email
Java Encryption
Java FTP
HTML-to-XML
Java HTTP
Java IMAP
Java MHT
Java MIME
Java RSA
Java S/MIME
Java Signatures
Java Socket
Java Spider
Java Tar
Java Upload
Java XML
Java XMP
Java Zip

More Examples...
Email Object
POP3
SMTP
RSS
Atom
String
Byte Array
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA
SSH Key
SSH
SSH Tunnel
SFTP

Unreleased...
Bzip2
LZW
Bz2
Icon

 

 

 

 

 

 

 

Export certificates and public/private keys from a PFX

Demonstrates how to export certificates and public/private keys from a PFX file.

Download Chilkat Java Library

import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success;
    CkCertStore certStore = new CkCertStore();

    //  Load the PFX file into a certificate store object
    String password;
    password = "myPassword";
    success = certStore.LoadPfxFile("chilkat.pfx",password);
    if (success != true) {
        System.out.println(certStore.lastErrorText());
        return;
    }

    int i;
    int numCerts;
    numCerts = (int) certStore.get_NumCertificates();

    CkCert cert;
    String fname;
    for (i = 0; i <= numCerts - 1; i++) {

        cert = certStore.GetCertificate(i);

        System.out.println(cert.subjectDN());
        System.out.println("---");

        //  Save the cert in DER format:
        fname = "cert" + Integer.toString(i) + ".der";
        cert.ExportCertDerFile(fname);

        //  Save the cert in PEM format:
        fname = "cert" + Integer.toString(i) + ".pem";
        cert.ExportCertPemFile(fname);

        //  Does this cert have a private key?
        if (cert.HasPrivateKey() == true) {

            //  Get the private key.
            CkPrivateKey pvkey;
            pvkey = cert.ExportPrivateKey();

            //  Save the private key to a PKCS8 DER-encoded file
            fname = "pvkey" + Integer.toString(i) + "_pkcs8.der";
            pvkey.SavePkcs8File(fname);

            //  Save the private key to a PKCS8 PEM-encoded file
            fname = "pvkey" + Integer.toString(i) + "_pkcs8.pem";
            pvkey.SavePkcs8PemFile(fname);

            //  Save the private key to a RSA DER-encoded file
            fname = "pvkey" + Integer.toString(i) + "_rsa.der";
            pvkey.SaveRsaDerFile(fname);

            //  Save the private key to a RSA PEM-encoded file
            fname = "pvkey" + Integer.toString(i) + "_rsa.pem";
            pvkey.SaveRsaPemFile(fname);

            //  Save the private key to an XML file
            //  This format is Chilkat-specific, but easily parsed,
            //  making it easy to get the modulus, exponent,
            //  P, Q, DP, DQ, InverseQ, and D.
            fname = "pvkey" + Integer.toString(i) + ".xml";
            pvkey.SaveXmlFile(fname);

        }

        //  Now get the public key and save it to various file formats:
        CkPublicKey pubkey;
        pubkey = cert.ExportPublicKey();

        //  Save to an OpenSSL DER format file:
        fname = "pubkey" + Integer.toString(i) + "_openSsl.der";
        pubkey.SaveOpenSslDerFile(fname);

        //  Save to an OpenSSL PEM format file:
        fname = "pubkey" + Integer.toString(i) + "_openSsl.pem";
        pubkey.SaveOpenSslPemFile(fname);

        //  Save to an RSA DER format file:
        fname = "pubkey" + Integer.toString(i) + "_rsa.der";
        pubkey.SaveRsaDerFile(fname);

        //  Save to an XML file:
        //  This format is Chilkat-specific, but easily parsed,
        //  making it easy to get the modulus and exponent.
        fname = "pubkey" + Integer.toString(i) + ".xml";
        pubkey.SaveXmlFile(fname);

    }

    //  The Chilkat Certificate, Certificate Store, Private Key,
    //  Public Key, and Key Container classes / objects are freeware.

    //  They are used by and included with the Chilkat Email,
    //  Crypt, S/MIME, and other commercial Chilkat components.

  }
}

 

Need a specific example? Send a request to support@chilkatsoft.com

© 2000-2008 Chilkat Software, Inc. All Rights Reserved.