Dart Requires Chilkat v11.0.0+
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
var success = false;
final certStore = CkCertStore();
final readOnly = true;
success = true;
try {
certStore.openCurrentUserStore(readOnly);
} on ChilkatException {
success = false;
}
if (!success) {
print(certStore.lastErrorText);
return;
}
final pfxPassword = 'secret';
var allSuccess = true;
var numSuccess = 0;
final zip = CkZip();
zip.newZip('qa_output/personalCerts.zip');
final certData = CkBinData();
final sbFilename = CkStringBuilder();
// Iterate over the certificates in the Current User store.
final cert = CkCert();
final numCerts = certStore.numCertificates;
var i = 0;
while (i < numCerts) {
certStore.getCert(i, cert);
print('DN = ${cert.subjectDN}');
sbFilename.setString('cert');
sbFilename.appendInt(i + 1);
final bHasPrivateKey = cert.hasPrivateKey();
if ((bHasPrivateKey) && (cert.privateKeyExportable)) {
// Export to a .pfx
success = true;
try {
cert.exportToPfxBd(pfxPassword, true, certData);
} on ChilkatException {
success = false;
}
if (success) {
sbFilename.append('.pfx');
zip.addBd(sbFilename.getAsString(), certData);
}
} else {
// Export to a .cer
success = true;
try {
cert.exportCertDerBd(certData);
} on ChilkatException {
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 {
zip.writeZipAndClose();
} on ChilkatException {
success = false;
}
if (!success) {
print(zip.lastErrorText);
allSuccess = false;
}
}
print('All success = $allSuccess');
}