Sample code for 30+ languages & platforms
Unicode C

Export a PFX as PEM Text

See more PFX/P12 Examples

Demonstrates Pfx.ToPem, which exports all private keys and certificates currently held by the object as PEM-formatted text.

Background. Certificates are written as standard BEGIN CERTIFICATE blocks. This converts PKCS#12 material into the widely used PEM format.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkPfxW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPfxW pfx;
    const wchar_t *password;
    const wchar_t *pem;

    success = FALSE;

    pfx = CkPfxW_Create();

    //  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.
    //  The PFX password should come from a secure source rather than being hard-coded.
    password = L"myPfxPassword";
    success = CkPfxW_LoadPfxFile(pfx,L"qa_data/identity.pfx",password);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPfxW_lastErrorText(pfx));
        CkPfxW_Dispose(pfx);
        return;
    }

    //  Export all private keys and certificates currently held by the object as PEM-formatted text.
    pem = CkPfxW_toPem(pfx);
    if (CkPfxW_getLastMethodSuccess(pfx) == FALSE) {
        wprintf(L"%s\n",CkPfxW_lastErrorText(pfx));
        CkPfxW_Dispose(pfx);
        return;
    }

    wprintf(L"%s\n",pem);


    CkPfxW_Dispose(pfx);

    }