Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
Extract Public/Private Keys and Certs from PFX into String VariablesDemonstrates how to export certificates and public/private keys from a PFX file into in-memory strings. LOCAL lnSuccess LOCAL loCertStore LOCAL lcPassword LOCAL i LOCAL lnNumCerts LOCAL loCert LOCAL lcFname LOCAL lcEncodedCert LOCAL loCert2 LOCAL loPvkey LOCAL lcPemPvKey LOCAL lcPkcs8PvKey LOCAL lcXmlPvKey LOCAL loPvKey2 LOCAL loPubkey LOCAL lcPubKeyPem LOCAL lcPubKeyXml LOCAL loPubKey2 loCertStore = CreateObject('Chilkat.CertStore') * Load the PFX file into a certificate store object lcPassword = "*myPassword2*" lnSuccess = loCertStore.LoadPfxFile("chilkat.pfx",lcPassword) IF (lnSuccess <> 1) THEN =MESSAGEBOX(loCertStore.LastErrorText) QUIT ENDIF lnNumCerts = loCertStore.NumCertificates * Loop over each certificate in the PFX. FOR i = 0 TO lnNumCerts - 1 loCert = loCertStore.GetCertificate(i) ? loCert.SubjectDN ? "---" lcEncodedCert = loCert.GetEncoded() * This string may now be stored in a relational database string field. * To re-create the cert, do this: loCert2 = CreateObject('Chilkat.Cert') loCert2.SetFromEncoded(lcEncodedCert) * Does this cert have a private key? IF (loCert.HasPrivateKey() = 1) THEN * Get the private key. loPvkey = loCert.ExportPrivateKey() * The private key can be exported into * a string in PKCS8, RSA PEM, or XML format: lcPemPvKey = loPvkey.GetRsaPem() lcPkcs8PvKey = loPvkey.GetPkcs8Pem() lcXmlPvKey = loPvkey.GetXml() ? lcPemPvKey ? lcPkcs8PvKey ? lcXmlPvKey * Any of these formatted strings may * be stored in a relational database field. * to restore, call LoadPem or LoadXml * LoadPem accepts either RSA PEM or * PKCS8 PEM: loPvKey2 = CreateObject('Chilkat.PrivateKey') loPvKey2.LoadPem(lcPemPvKey) loPvKey2.LoadPem(lcPkcs8PvKey) loPvKey2.LoadXml(lcXmlPvKey) RELEASE loPvkey ENDIF * Now for the public key: loPubkey = loCert.ExportPublicKey() * It can be exported to a string as OpenSSL PEM * or XML: lcPubKeyPem = loPubkey.GetOpenSslPem() lcPubKeyXml = loPubkey.GetXml() ? lcPubKeyPem ? lcPubKeyXml * To re-load a PublicKey object, call LoadXml * or LoadOpenSslPem: loPubKey2 = CreateObject('Chilkat.PublicKey') loPubKey2.LoadOpenSslPem(lcPubKeyPem) loPubKey2.LoadXml(lcPubKeyXml) lcFname = "pubkey" + STR(i) + "_openSsl.der" loPubkey.SaveOpenSslDerFile(lcFname) RELEASE loPubkey RELEASE loCert NEXT * The Chilkat Certificate, Certificate Store, Private Key, * Public Key, and Key Container classes / objects are freeware. * They are used by and included with the Chilkat Email, * Crypt, S/MIME, and other commercial Chilkat components. |
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2007 Chilkat Software, Inc. All Rights Reserved.
Mail Component · .NET Email Component · ASP Mail Component · XML Parser