Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

Backup Windows Current User / Personal Certificates to a .zip

See more Certificates Examples

Demonstrates how to backup the certificates in the Windows registry-based Current User certificate store (in the "Personal" Logical Store as seen in certmgr.msc), to a zip archive. Certificates having an exportable private key are exported to .pfx files. Certificates with no private key, or with a non-exportable private key, are exported to .cer files.

Obviously, this example only runs on Windows computers.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.CertStore;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.StringBuilder;
import com.chilkatsoft.chilkat2.Zip;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        CertStore certStore = new CertStore();

        boolean readOnly = true;
        try {
            certStore.openCurrentUserStore(readOnly);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        String pfxPassword = "secret";

        boolean allSuccess = true;
        int numSuccess = 0;

        Zip zip = new Zip();
        zip.newZip("qa_output/personalCerts.zip");

        BinData certData = new BinData();
        StringBuilder sbFilename = new StringBuilder();

        // Iterate over the certificates in the Current User store.
        Cert cert = new Cert();
        int numCerts = certStore.getNumCertificates();
        int i = 0;
        while (i < numCerts) {
            certStore.getCert(i, cert);
            System.out.println("DN = " + cert.getSubjectDN());

            sbFilename.setString("cert");
            sbFilename.appendInt(i + 1);

            boolean bHasPrivateKey = cert.hasPrivateKey();
            if ((bHasPrivateKey) && (cert.getPrivateKeyExportable())) {
                // Export to a .pfx
                try {
                    cert.exportToPfxBd(pfxPassword, true, certData);
                    success = true;
                } catch (ChilkatException e) {
                    success = false;
                }
                if (success) {
                    sbFilename.append(".pfx");
                    zip.addBd(sbFilename.getAsString(), certData);
                }
            } else {
                // Export to a .cer
                try {
                    cert.exportCertDerBd(certData);
                    success = true;
                } catch (ChilkatException e) {
                    success = false;
                }
                if (success) {
                    sbFilename.append(".cer");
                    zip.addBd(sbFilename.getAsString(), certData);
                }
            }

            if (!success) {
                allSuccess = false;
            } else {
                numSuccess = numSuccess + 1;
            }

            i = i + 1;
        }

        if (numSuccess > 0) {
            try {
                zip.writeZipAndClose();
            } catch (ChilkatException e) {
                System.out.println(e.getLastErrorText());
                allSuccess = false;
            }
        }

        System.out.println("All success = " + allSuccess);
    }
}