Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) Export a Private Key from an MS Storage ProviderDemonstrates how to export a private key from a Microsoft Storage Provider. Note: This example requires Chilkat v9.5.0.83 or greater.
integer li_rc oleobject loo_Cert integer li_Success oleobject loo_PrivKey string ls_StorageProvider string ls_KeyName oleobject loo_KeyCon oleobject loo_PrivKey2 integer li_SilentFlag // This example requires Chilkat v9.5.0.83 or greater. // We'll export a certificate's private key from the MS storage provider. // It is assumed the certificate + private key is already installed on the Windows system. // The export does not remove the key from the Windows storage provider. // First, let's get a certificate in one of the many ways we can do it. // (I ran certmgr.msc, opened a certificate, and noted it's thumbprint.) loo_Cert = create oleobject // Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0 li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert") if li_rc < 0 then destroy loo_Cert MessageBox("Error","Connecting to COM object failed") return end if li_Success = loo_Cert.LoadByThumbprint("ea5a129c1919a52d238ee28d9f3a8f345b768388","hex") if li_Success = 0 then Write-Debug loo_Cert.LastErrorText destroy loo_Cert return end if Write-Debug "Found: " + loo_Cert.SubjectDN // First export the private key the easy way. loo_PrivKey = loo_Cert.ExportPrivateKey() if loo_Cert.LastMethodSuccess = 0 then Write-Debug loo_Cert.LastErrorText destroy loo_Cert return end if // OK.. we have the private key. Do whatever we want with it.. destroy loo_PrivKey // ------------------------------------------------------------- // Now let's export in a more roundabout way by getting information about the // storage provider and key name and then we'll export completely independent // of the certificate. // // Remember: The private key is not contained within the certificate. An X.509 certificate // embeds the public key, but the counterpart private key is stored elsewhere, such // as in a .pfx/.p12, or as in this case, in the Windows "protected store", or perhaps on // a smartcard or hardware token. (But a private key stored on a smartcard or token cannot // be exported. It must remain on the hardware.) // ls_StorageProvider = loo_Cert.CspName ls_KeyName = loo_Cert.KeyContainerName Write-Debug "Information about the certificate's private key:" Write-Debug "Storage Provider: " + ls_StorageProvider Write-Debug "Key Name: " + ls_KeyName // Export using just the name of the storage provider and key. loo_KeyCon = create oleobject // Use "Chilkat_9_5_0.KeyContainer" for versions of Chilkat < 10.0.0 li_rc = loo_KeyCon.ConnectToNewObject("Chilkat.KeyContainer") loo_PrivKey2 = create oleobject // Use "Chilkat_9_5_0.PrivateKey" for versions of Chilkat < 10.0.0 li_rc = loo_PrivKey2.ConnectToNewObject("Chilkat.PrivateKey") li_SilentFlag = 0 li_Success = loo_KeyCon.ExportKey(ls_KeyName,ls_StorageProvider,li_SilentFlag,loo_PrivKey2) if li_Success = 0 then Write-Debug loo_KeyCon.LastErrorText destroy loo_Cert destroy loo_KeyCon destroy loo_PrivKey2 return end if // OK.. we have the private key in privKey2. Do whatever we want with it.. // Perhaps we save as encrypted PKCS8 PEM. li_Success = loo_PrivKey2.SavePkcs8EncryptedPemFile("myPassword","qa_output/privKey2.pem") if li_Success = 0 then Write-Debug loo_PrivKey2.LastErrorText destroy loo_Cert destroy loo_KeyCon destroy loo_PrivKey2 return end if Write-Debug "Success." destroy loo_Cert destroy loo_KeyCon destroy loo_PrivKey2 |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.