Sample code for 30+ languages & platforms
React Native 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 React Native Downloads

React Native
import { BinData, Cert, CertStore, StringBuilder, Zip } from '@chilkat/react-native'

async function chilkatExample() {
  let success = false;

  const certStore = new CertStore();

  const readOnly = true;
  success = true;
  try {
    certStore.openCurrentUserStore(readOnly);
  } catch {
    success = false;
  }
  if (!success) {
    console.log(certStore.lastErrorText);
    return;
  }

  const pfxPassword = 'secret';

  let allSuccess = true;
  let numSuccess = 0;

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

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

  // Iterate over the certificates in the Current User store.
  const cert = new Cert();
  const numCerts = certStore.numCertificates;
  let i = 0;
  while (i < numCerts) {
    certStore.getCert(i, cert);
    console.log(`DN = ${cert.subjectDN}`);

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

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

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

    i++;
  }

  if (numSuccess > 0) {
    success = true;
    try {
      await zip.writeZipAndCloseAsync();
    } catch {
      success = false;
    }
    if (!success) {
      console.log(zip.lastErrorText);
      allSuccess = false;
    }
  }

  console.log(`All success = ${allSuccess}`);
}