Sample code for 30+ languages & platforms
Visual FoxPro

Load Identities from PEM Text

See more PFX/P12 Examples

Demonstrates Pfx.LoadPem, which loads one or more certificate/private-key identities from PEM-formatted text and builds the contents of the Pfx object.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. The password decrypts any encrypted private keys in the PEM. This builds a PFX in memory from PEM material, which can then be serialized to PKCS#12.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loPfx
LOCAL loSbPem
LOCAL lnBLoaded
LOCAL lcPemText
LOCAL lcPassword

lnSuccess = 0

loPfx = CreateObject('Chilkat.Pfx')

*  The file path is relative to the application's current working directory.  An absolute path
*  may also be used.  Supply the path appropriate to your own environment.
*  Load the PEM text (certificates and private keys) into a string.
loSbPem = CreateObject('Chilkat.StringBuilder')
lnBLoaded = loSbPem.LoadFile("qa_data/identity.pem")
IF (lnBLoaded <> 1) THEN
    ? "Failed to load the PEM file."
    RELEASE loPfx
    RELEASE loSbPem
    CANCEL
ENDIF

lcPemText = loSbPem.GetAsString()
IF (loSbPem.LastMethodSuccess = 0) THEN
    ? loSbPem.LastErrorText
    RELEASE loPfx
    RELEASE loSbPem
    CANCEL
ENDIF

*  The password decrypts any encrypted private keys in the PEM (use an empty string if none are
*  encrypted).  A password should come from a secure source rather than being hard-coded.
lcPassword = "myPemPassword"
lnSuccess = loPfx.LoadPem(lcPemText,lcPassword)
IF (lnSuccess = 0) THEN
    ? loPfx.LastErrorText
    RELEASE loPfx
    RELEASE loSbPem
    CANCEL
ENDIF

? "Loaded " + STR(loPfx.NumPrivateKeys) + " private key(s)."

RELEASE loPfx
RELEASE loSbPem