Xbase++
Xbase++
Add Private Key and Certificate to a PEM
See more PEM Examples
Demonstrates how to add certificates and private keys to a PEM.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oPem
LOCAL oPrivKey
LOCAL oCert
LOCAL nIncludeCertChain
LOCAL cPassword
LOCAL nExtendedAttrs
LOCAL nNoKeys
LOCAL nNoCerts
LOCAL nNoCaCerts
LOCAL cEncryptAlg
LOCAL cPemStr
nSuccess := 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The Chilkat PEM class was introduced in v9.5.0.49.
// It requires the bundle to be unlocked, as shown above.
oPem := CreateObject("Chilkat.Pem")
// Add the private key found in alice.key to this PEM.
//
oPrivKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oPrivKey:LoadAnyFormatFile("qa_data/alice.key", "")
IF (nSuccess != 1)
? oPrivKey:LastErrorText
oPem:destroy()
oPrivKey:destroy()
RETURN
ENDIF
// Add it to the PEM:
nSuccess := oPem:AddPrivateKey(oPrivKey)
// Add the certificate found in alice.crt to this PEM.
//
oCert := CreateObject("Chilkat.Cert")
nSuccess := oCert:LoadFromFile("qa_data/alice.crt")
IF (nSuccess != 1)
? oCert:LastErrorText
oPem:destroy()
oPrivKey:destroy()
oCert:destroy()
RETURN
ENDIF
// Add it to the PEM:
nIncludeCertChain := 0
nSuccess := oPem:AddCert(oCert, nIncludeCertChain)
// Write the PEM containing the private key and certificate.
// The private key will be output in PKCS8 encrypted form.
// Certificates are never encrypted.
// This is the password that will be required to open and access the private key
// from the PEM we're about to write..
cPassword := "secret"
nExtendedAttrs := 0
nNoKeys := 0
nNoCerts := 0
nNoCaCerts := 0
cEncryptAlg := "aes128"
cPemStr := oPem:ToPemEx(nExtendedAttrs, nNoKeys, nNoCerts, nNoCaCerts, cEncryptAlg, cPassword)
? cPemStr
oPem:destroy()
oPrivKey:destroy()
oCert:destroy()