Sample code for 30+ languages & platforms
Unicode C

MIME S/MIME Encryption Algorithm Properties

See more MIME Examples

Demonstrates the properties that control S/MIME encryption: Pkcs7CryptAlg (the symmetric content-encryption algorithm), Pkcs7KeyLength (the content-encryption key length in bits), OaepPadding (whether RSAES-OAEP key transport is used instead of RSAES-PKCS1-v1_5), and the OAEP hash settings OaepHash and OaepMgfHash. The properties are configured before calling Encrypt.

Background. CMS/PKCS #7 encryption uses a symmetric algorithm to protect the content and an RSA key-transport scheme to protect the symmetric key. The defaults (aes, 128-bit, PKCS1-v1_5 padding) work broadly; these properties tune the algorithms and padding.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkMimeW mime;
    HCkCertW cert;

    success = FALSE;

    mime = CkMimeW_Create();
    success = CkMimeW_SetBodyFromPlainText(mime,L"This message will be encrypted.");
    if (success == FALSE) {
        wprintf(L"%s\n",CkMimeW_lastErrorText(mime));
        CkMimeW_Dispose(mime);
        return;
    }

    //  Pkcs7CryptAlg is the symmetric content-encryption algorithm.  Supported values are aes, aes-gcm,
    //  des, 3des, and rc2.  The default is aes.
    CkMimeW_putPkcs7CryptAlg(mime,L"aes");

    //  Pkcs7KeyLength is the content-encryption key length in bits.  The default is 128.
    CkMimeW_putPkcs7KeyLength(mime,256);

    //  OaepPadding selects the RSA key-transport padding.  The default is FALSE (RSAES-PKCS1-v1_5).
    //  Set TRUE to use RSAES-OAEP, in which case the OAEP hash settings apply.
    CkMimeW_putOaepPadding(mime,TRUE);
    CkMimeW_putOaepHash(mime,L"sha256");
    CkMimeW_putOaepMgfHash(mime,L"sha256");

    //  Load the recipient certificate (public key is sufficient for encrypting).
    cert = CkCertW_Create();
    success = CkCertW_LoadFromFile(cert,L"qa_data/recipient.cer");
    if (success == FALSE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkMimeW_Dispose(mime);
        CkCertW_Dispose(cert);
        return;
    }

    //  Encrypt using the algorithm settings configured above.
    success = CkMimeW_Encrypt(mime,cert);
    if (success == FALSE) {
        wprintf(L"%s\n",CkMimeW_lastErrorText(mime));
        CkMimeW_Dispose(mime);
        CkCertW_Dispose(cert);
        return;
    }

    wprintf(L"Encrypted with %s %d-bit key.\n",CkMimeW_pkcs7CryptAlg(mime),CkMimeW_getPkcs7KeyLength(mime));


    CkMimeW_Dispose(mime);
    CkCertW_Dispose(cert);

    }