private async void ExportAsync() { try { if (!this.ValidateControls()) { UpdateStatusStrip("Please fill all required fields."); return; } UpdateStatusStrip(string.Empty); ToogleControls(enabled: false); byte[] cryptographicObjectContent = null; var saveFileDialogFilter = string.Empty; var saveFileDialogFileName = Path.GetFileNameWithoutExtension(this.textBoxPath.Text); var selectedItemObject = (string)this.comboBoxObject.SelectedItem; var selectedItemFormat = (string)this.comboBoxFormat.SelectedItem; var selectedCryptographicObjectType = CertUtilConstants.CryptographicObjects[selectedItemObject]; await Task.Run(() => { if (selectedCryptographicObjectType == CertUtilConstants.CryptographicObjectType.PublicKeyCertificate) { saveFileDialogFilter = string.Format("{0}|{1}", selectedItemFormat, CertUtilConstants.PublicKeyCertificatesFormats[selectedItemFormat]); saveFileDialogFileName += CertUtilConstants.PublicKeyCertificatesFormats[selectedItemFormat].Trim('*'); if (selectedItemFormat.Contains("Base64")) { var cryptographicObjectText = CertificateUtils.ExportPublicKeyCertificateToPEM(File.ReadAllBytes(this.textBoxPath.Text), this.textBoxPassword.Text.ToSecureString()); cryptographicObjectContent = System.Text.Encoding.ASCII.GetBytes(cryptographicObjectText); } else if (selectedItemFormat.Contains("Binary")) { cryptographicObjectContent = CertificateUtils.ExportPublicKeyCertificate(File.ReadAllBytes(this.textBoxPath.Text), this.textBoxPassword.Text.ToSecureString()); } else { throw new NotImplementedException(); } } else if (selectedCryptographicObjectType == CertUtilConstants.CryptographicObjectType.PrivateKey) { saveFileDialogFilter = string.Format("{0}|{1}", selectedItemFormat, CertUtilConstants.PrivateKeyFormats[selectedItemFormat]); saveFileDialogFileName += CertUtilConstants.PrivateKeyFormats[selectedItemFormat].Trim('*'); if (selectedItemFormat.Contains("Base64")) { var cryptographicObjectText = CertificateUtils.ExportPrivateKeyToPEM(File.ReadAllBytes(this.textBoxPath.Text), this.textBoxPassword.Text.ToSecureString()); if (cryptographicObjectText.IsNullOrEmpty()) { throw new Exception("Certificate does not have a private key."); } cryptographicObjectContent = System.Text.Encoding.ASCII.GetBytes(cryptographicObjectText); } else if (selectedItemFormat.Contains("Binary")) { cryptographicObjectContent = CertificateUtils.ExportPrivateKey(File.ReadAllBytes(this.textBoxPath.Text), this.textBoxPassword.Text.ToSecureString()); if (cryptographicObjectContent == null) { throw new Exception("Certificate does not have a private key."); } } else { throw new NotImplementedException(); } } else { throw new NotImplementedException(); } }); using (var saveFileDialog = new SaveFileDialog()) { saveFileDialog.Title = "Please choose a file name..."; saveFileDialog.Filter = saveFileDialogFilter; saveFileDialog.FileName = saveFileDialogFileName; saveFileDialog.CheckFileExists = false; saveFileDialog.CheckPathExists = true; if (DialogResult.OK == saveFileDialog.ShowDialog(this)) { string fileName = saveFileDialog.FileName; if (fileName.Length > 0) { File.WriteAllBytes(fileName, cryptographicObjectContent); UpdateStatusStrip("File has been successfully saved."); } } } } catch (Exception ex) { MessageBox.Show(this, $"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } ToogleControls(enabled: true); }