Sample code for 30+ languages & platforms
Visual FoxPro

Add Private Key and Certificate to a PEM

See more PEM Examples

Demonstrates how to add certificates and private keys to a PEM.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loPem
LOCAL loPrivKey
LOCAL loCert
LOCAL lnIncludeCertChain
LOCAL lcPassword
LOCAL lnExtendedAttrs
LOCAL lnNoKeys
LOCAL lnNoCerts
LOCAL lnNoCaCerts
LOCAL lcEncryptAlg
LOCAL lcPemStr

lnSuccess = 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.
loPem = CreateObject('Chilkat.Pem')

* Add the private key found in alice.key to this PEM.
* 
loPrivKey = CreateObject('Chilkat.PrivateKey')

lnSuccess = loPrivKey.LoadAnyFormatFile("qa_data/alice.key","")
IF (lnSuccess <> 1) THEN
    ? loPrivKey.LastErrorText
    RELEASE loPem
    RELEASE loPrivKey
    CANCEL
ENDIF

* Add it to the PEM:
lnSuccess = loPem.AddPrivateKey(loPrivKey)

* Add the certificate found in alice.crt to this PEM.
* 
loCert = CreateObject('Chilkat.Cert')

lnSuccess = loCert.LoadFromFile("qa_data/alice.crt")
IF (lnSuccess <> 1) THEN
    ? loCert.LastErrorText
    RELEASE loPem
    RELEASE loPrivKey
    RELEASE loCert
    CANCEL
ENDIF

* Add it to the PEM:
lnIncludeCertChain = 0
lnSuccess = loPem.AddCert(loCert,lnIncludeCertChain)

* 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..
lcPassword = "secret"
lnExtendedAttrs = 0
lnNoKeys = 0
lnNoCerts = 0
lnNoCaCerts = 0
lcEncryptAlg = "aes128"

lcPemStr = loPem.ToPemEx(lnExtendedAttrs,lnNoKeys,lnNoCerts,lnNoCaCerts,lcEncryptAlg,lcPassword)
? lcPemStr

RELEASE loPem
RELEASE loPrivKey
RELEASE loCert