Sample code for 30+ languages & platforms
Unicode C Requires Chilkat v11.8.0+

PKCS7 Decrypt MIME

See more MIME Examples

Loads a PKCS7 encrypted MIME file and decrypts. The cert and private key used for decryption is loaded from a PEM file.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkMimeW.h>
#include <C_CkPemW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkMimeW mime;
    const wchar_t *pemPassword;
    HCkPemW pem;

    success = FALSE;

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

    mime = CkMimeW_Create();

    //  Load the MIME
    success = CkMimeW_LoadMimeFile(mime,L"encryptedMime.txt");
    if (success == FALSE) {
        wprintf(L"%s\n",CkMimeW_lastErrorText(mime));
        CkMimeW_Dispose(mime);
        return;
    }

    //  The AddPemSource method may be called one or more times
    //  (one per Pem object) to add sources from which the MIME component will
    //  search for certificates and private keys when decrypting.
    //  Here the certificate and its private key are loaded from a PEM file.
    //  The 2nd argument of LoadPemFile is the password of the private key.
    pemPassword = L"myPassword";
    pem = CkPemW_Create();
    success = CkPemW_LoadPemFile(pem,L"myCertAndPrivateKey.pem",pemPassword);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPemW_lastErrorText(pem));
        CkMimeW_Dispose(mime);
        CkPemW_Dispose(pem);
        return;
    }

    success = CkMimeW_AddPemSource(mime,pem);
    if (success == FALSE) {
        wprintf(L"%s\n",CkMimeW_lastErrorText(mime));
        CkMimeW_Dispose(mime);
        CkPemW_Dispose(pem);
        return;
    }

    //  Decrypt...
    success = CkMimeW_Decrypt(mime);
    if (success == FALSE) {
        wprintf(L"%s\n",CkMimeW_lastErrorText(mime));
        CkMimeW_Dispose(mime);
        CkPemW_Dispose(pem);
        return;
    }

    //  Display the decrypted MIME:
    wprintf(L"%s\n",CkMimeW_getMime(mime));


    CkMimeW_Dispose(mime);
    CkPemW_Dispose(pem);

    }