Sample code for 30+ languages & platforms
Unicode C

Set the Decryption Certificate and Private Key

See more Email Object Examples

Demonstrates the Chilkat Email.SetDecryptCert2 method, which explicitly provides a certificate and its corresponding private key as separate objects for decrypting a received encrypted email. Set them before loading the encrypted message. This example loads a .cer certificate and a PEM private key, sets both, then loads an encrypted email.

Background: Sometimes the certificate and its private key are stored separately — a public .cer file plus a PEM (or other format) key file — rather than combined in a PFX. SetDecryptCert2 accepts the two objects individually, which is the natural fit for that arrangement. It is the two-object counterpart to SetDecryptCert, which takes a single certificate that already carries its private key.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkEmailW.h>
#include <C_CkCertW.h>
#include <C_CkPrivateKeyW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmailW email;
    HCkCertW cert;
    HCkPrivateKeyW privKey;

    success = FALSE;

    //  Demonstrates the SetDecryptCert2 method, which explicitly provides a certificate and its
    //  corresponding private key (as separate objects) for decrypting a received encrypted
    //  email.  Set them before loading the encrypted email.

    email = CkEmailW_Create();

    //  Load the certificate (public) and the matching private key from separate files.
    cert = CkCertW_Create();
    success = CkCertW_LoadFromFile(cert,L"qa_data/certs/recipient.cer");
    if (success == FALSE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkEmailW_Dispose(email);
        CkCertW_Dispose(cert);
        return true;
    }

    privKey = CkPrivateKeyW_Create();
    success = CkPrivateKeyW_LoadPemFile(privKey,L"qa_data/certs/recipient_privkey.pem");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(privKey));
        CkEmailW_Dispose(email);
        CkCertW_Dispose(cert);
        CkPrivateKeyW_Dispose(privKey);
        return true;
    }

    //  Provide the certificate and private key to use for decryption.
    success = CkEmailW_SetDecryptCert2(email,cert,privKey);
    if (success == FALSE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkEmailW_Dispose(email);
        CkCertW_Dispose(cert);
        CkPrivateKeyW_Dispose(privKey);
        return true;
    }

    //  Load the encrypted email; Chilkat decrypts it using the certificate and key.
    success = CkEmailW_LoadEml(email,L"qa_data/eml/encrypted.eml");
    if (success == FALSE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkEmailW_Dispose(email);
        CkCertW_Dispose(cert);
        CkPrivateKeyW_Dispose(privKey);
        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);
    CkCertW_Dispose(cert);
    CkPrivateKeyW_Dispose(privKey);

    }