Sample code for 30+ languages & platforms
Xbase++

Duplicate openssl pkcs12 –export –in certfile.cer –inkey certfile.key –out certfile.pfx

See more OpenSSL Examples

How to create a PKCS12 (.p12 or .pfx) from a certificate file and private key file: Demonstrates how to duplicate this OpenSSL command:
Duplicate openssl pkcs12 –export –in certfile.cer –inkey certfile.key –out certfile.pfx

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oPkey
LOCAL oCert
LOCAL oCertChain
LOCAL oPfx
LOCAL cPassword

nSuccess := 0

//  This example assumes the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oPkey := CreateObject("Chilkat.PrivateKey")

//  Load the private key from the file.
nSuccess := oPkey:LoadAnyFormatFile("certFile.key", "")
IF (nSuccess != 1)
    ? oPkey:LastErrorText
    oPkey:destroy()
    RETURN
ENDIF

oCert := CreateObject("Chilkat.Cert")
//  The LoadFromFile method auto-recognizes the format...
nSuccess := oCert:LoadFromFile("certfile.cer")
IF (nSuccess != 1)
    ? oCert:LastErrorText
    oPkey:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  We'll need a cert chain object to create the PKCS12, so get it
//  from the cert.  

oCertChain := oCert:GetCertChain()
IF (.NOT. oCert:LastMethodSuccess)
    ? oCert:LastErrorText
    oPkey:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  Create the PFX object, add the cert and private key, and write to a .pfx file.
oPfx := CreateObject("Chilkat.Pfx")

//  The cert(s) are automatically added in the call to AddPrivateKey
nSuccess := oPfx:AddPrivateKey(oPkey, oCertChain)
IF (nSuccess != 1)
    ? oPfx:LastErrorText
    oPkey:destroy()
    oCert:destroy()
    oPfx:destroy()
    RETURN
ENDIF

//  Write the .pfx to a file.
cPassword := "myPassword"
nSuccess := oPfx:ToFile(cPassword, "certfile.pfx")
IF (nSuccess != 1)
    ? oPfx:LastErrorText
    oPkey:destroy()
    oCert:destroy()
    oPfx:destroy()
    RETURN
ENDIF

? "Success."

oPkey:destroy()
oCert:destroy()
oPfx:destroy()