Xbase++ Requires Chilkat v11.0.0+
Xbase++
Export a Certificate's Private Key to Various Formats
See more Certificates Examples
Loads a digital certificate and private key from a PFX file (also known as PKCS#12) and exports the private key to various formats: (1) PKCS8 Encrypted, (2) PKCS8 Encrypted PEM, (3) PKCS8 unencrypted, (4) PKCS8 PEM unencrypted, (5) RSA DER unencrypted, (6) RSA PEM unencrypted, (7) XML.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oCert
LOCAL cPfxFilename
LOCAL cPfxPassword
LOCAL oPrivKey
LOCAL cPassword
LOCAL cPath
nSuccess := 0
oCert := CreateObject("Chilkat.Cert")
// Load from the PFX file
cPfxFilename := "/Users/chilkat/testData/pfx/chilkat_ssl_pwd_is_test.pfx"
cPfxPassword := "test"
// A PFX typically contains certificates in the chain of authentication.
// The Chilkat cert object will choose the certificate w/
// private key farthest from the root authority cert.
// To access all the certificates in a PFX, use the
// Chilkat certificate store object instead.
nSuccess := oCert:LoadPfxFile(cPfxFilename, cPfxPassword)
IF (nSuccess == 0)
? oCert:LastErrorText
oCert:destroy()
RETURN
ENDIF
// Get the private key...
oPrivKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oCert:GetPrivateKey(oPrivKey)
IF (nSuccess == 0)
? oCert:LastErrorText
oCert:destroy()
oPrivKey:destroy()
RETURN
ENDIF
// Export to various formats:
cPassword := "secret"
// PKCS8 Encrypted DER
cPath := "/Users/chilkat/testData/privkeys/chilkat_pkcs8_enc.der"
nSuccess := oPrivKey:SavePkcs8EncryptedFile(cPassword, cPath)
IF (nSuccess == 0)
? oPrivKey:LastErrorText
oCert:destroy()
oPrivKey:destroy()
RETURN
ENDIF
// PKCS8 Encrypted PEM
cPath := "/Users/chilkat/testData/privkeys/chilkat_pkcs8_enc.pem"
nSuccess := oPrivKey:SavePkcs8EncryptedPemFile(cPassword, cPath)
IF (nSuccess == 0)
? oPrivKey:LastErrorText
oCert:destroy()
oPrivKey:destroy()
RETURN
ENDIF
// PKCS8 Unencrypted DER
cPath := "/Users/chilkat/testData/privkeys/chilkat_pkcs8.der"
nSuccess := oPrivKey:SavePkcs8File(cPath)
IF (nSuccess == 0)
? oPrivKey:LastErrorText
oCert:destroy()
oPrivKey:destroy()
RETURN
ENDIF
// PKCS8 Unencrypted PEM
cPath := "/Users/chilkat/testData/privkeys/chilkat_pkcs8.pem"
nSuccess := oPrivKey:SavePkcs8PemFile(cPath)
IF (nSuccess == 0)
? oPrivKey:LastErrorText
oCert:destroy()
oPrivKey:destroy()
RETURN
ENDIF
// RSA DER (unencrypted)
cPath := "/Users/chilkat/testData/privkeys/chilkat_rsa.der"
nSuccess := oPrivKey:SavePkcs1File(cPath)
IF (nSuccess == 0)
? oPrivKey:LastErrorText
oCert:destroy()
oPrivKey:destroy()
RETURN
ENDIF
// RSA PEM (unencrypted)
cPath := "/Users/chilkat/testData/privkeys/chilkat_rsa.pem"
nSuccess := oPrivKey:SavePemFile(cPath)
IF (nSuccess == 0)
? oPrivKey:LastErrorText
oCert:destroy()
oPrivKey:destroy()
RETURN
ENDIF
// XML (unencrypted)
cPath := "/Users/chilkat/testData/privkeys/chilkat.xml"
nSuccess := oPrivKey:SaveXmlFile(cPath)
IF (nSuccess == 0)
? oPrivKey:LastErrorText
oCert:destroy()
oPrivKey:destroy()
RETURN
ENDIF
? "Private key exported to various formats."
oCert:destroy()
oPrivKey:destroy()