Java Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CMFCDelphiFoxProJavaPerl
PHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

Java Examples

Quick Start
Unicode
Bz2
Certificates
CSV
Email
Encryption
FTP
HTML Conversion
HTTP
IMAP
MHT
MIME
POP3
RSA
S/MIME
SFTP
Signatures
SMTP
Socket / SSL
Spider
SSH
SSH Key
SSH Tunnel
Tar
Upload
XML
XMP
Zip

More Examples...
Amazon S3
Email Object
DKIM / DomainKey
NTLM
FileAccess
RSS
Atom
String
Byte Array
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA
Bzip2
LZW

 

 

 

 

 

 

 

Export certificates and public/private keys from a PFX

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

 Chilkat Java Library Downloads for Windows, Linux, and MAC OS X

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.

  }
}

 

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