Sample code for 30+ languages & platforms
Unicode C++

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 Unicode C++ Downloads

Unicode C++
#include <CkPfxW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

    CkPfxW 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.
    CkStringBuilderW sbPem;
    bool bLoaded = sbPem.LoadFile(L"qa_data/identity.pem");
    if (bLoaded != true) {
        wprintf(L"Failed to load the PEM file.\n");
        return;
    }

    const wchar_t *pemText = sbPem.getAsString();
    if (sbPem.get_LastMethodSuccess() == false) {
        wprintf(L"%s\n",sbPem.lastErrorText());
        return;
    }

    //  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.
    const wchar_t *password = L"myPemPassword";
    success = pfx.LoadPem(pemText,password);
    if (success == false) {
        wprintf(L"%s\n",pfx.lastErrorText());
        return;
    }

    wprintf(L"Loaded %d private key(s).\n",pfx.get_NumPrivateKeys());
    }