Sample code for 30+ languages & platforms
Unicode C

Add a PFX Source from BinData

See more Email Object Examples

Demonstrates the Chilkat Email.AddPfxSourceBd method, which adds PFX data held in a BinData object to the list of certificate and private-key sources used during decryption or signing. The second argument is the PFX password. Call it before obtaining the encrypted email so Chilkat can decrypt it automatically. This example loads a PFX into a BinData, adds it, and then loads an encrypted email.

Background: This is the in-memory (BinData) counterpart to AddPfxSourceFile. It is the right choice when the PFX bytes come from somewhere other than a file — a database, a secrets manager, or an HTTP download — letting you supply the certificate and private key without first writing them to disk (which is preferable for sensitive key material).

Chilkat Unicode C Downloads

Unicode C
#include <C_CkEmailW.h>
#include <C_CkBinDataW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmailW email;
    HCkBinDataW bdPfx;

    success = FALSE;

    //  Demonstrates the AddPfxSourceBd method, which adds PFX data (held in a BinData) to the
    //  list of certificate and private-key sources used during decryption or signing.  The
    //  second argument is the PFX password.  Call it before obtaining the encrypted email.

    email = CkEmailW_Create();

    //  Load PFX data into a BinData object.
    bdPfx = CkBinDataW_Create();
    success = CkBinDataW_LoadFile(bdPfx,L"qa_data/certs/decryption.pfx");
    if (success == FALSE) {
        wprintf(L"%s\n",CkBinDataW_lastErrorText(bdPfx));
        CkEmailW_Dispose(email);
        CkBinDataW_Dispose(bdPfx);
        return true;
    }

    //  Add the PFX as a source of the certificate + private key.
    success = CkEmailW_AddPfxSourceBd(email,bdPfx,L"pfx_password");
    if (success == FALSE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkEmailW_Dispose(email);
        CkBinDataW_Dispose(bdPfx);
        return true;
    }

    //  Load an encrypted email; Chilkat decrypts it using the PFX source.
    success = CkEmailW_LoadEml(email,L"qa_data/eml/encrypted.eml");
    if (success == FALSE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkEmailW_Dispose(email);
        CkBinDataW_Dispose(bdPfx);
        return true;
    }

    wprintf(L"Decrypted = %d\n",CkEmailW_getDecrypted(email));

    //  Note: Paths such as "qa_data/..." are relative local filesystem paths,
    //  relative to the current working directory of the running application.


    CkEmailW_Dispose(email);
    CkBinDataW_Dispose(bdPfx);

    }