Sample code for 30+ languages & platforms
Unicode C

Encrypt a file to a PKCS7 encrypted message using multiple certificates from different users

See more Encryption Examples

Demonstrates how to encrypt a file to a PKCS7 encrypted message using multiple certificates from different users. Any one of the users can decrypt using his/her own certificate + private key.

Note: When doing public key encryption, it is the public key that is used to encrypt. The private key is required for decryption.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkCrypt2W.h>
#include <C_CkCertW.h>
#include <C_CkBinDataW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkCrypt2W crypt;
    HCkCertW cert1;
    HCkCertW cert2;
    HCkCertW cert3;
    HCkBinDataW fileData;

    success = FALSE;

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

    crypt = CkCrypt2W_Create();

    // Tell the crypt object to use 3 certificates.
    // Do this by calling AddEncryptCert for each certificate.

    // Load a digital certificate. 
    // We don't need the private key for encryption.
    // Only the public key is needed (which is included in a certificate).
    cert1 = CkCertW_Create();
    success = CkCertW_LoadFromFile(cert1,L"qa_data/user1/cert_user1.pem");
    // Assume success for the example, but make sure your application checks for success/failure...
    CkCrypt2W_AddEncryptCert(crypt,cert1);

    cert2 = CkCertW_Create();
    success = CkCertW_LoadFromFile(cert2,L"qa_data/user2/cert_user2.pem");
    CkCrypt2W_AddEncryptCert(crypt,cert2);

    cert3 = CkCertW_Create();
    success = CkCertW_LoadFromFile(cert3,L"qa_data/user3/cert_user3.pem");
    CkCrypt2W_AddEncryptCert(crypt,cert3);

    // Indicate that we want PKI encryption (i.e. public-key infrastructure)
    // to produce a CMS message (Cryptographic Message Syntax/PKCS7),
    // that is be created with RSAES-OAEP padding, SHA256, and AES-128 for the
    // bulk encryption.
    CkCrypt2W_putCryptAlgorithm(crypt,L"pki");
    CkCrypt2W_putPkcs7CryptAlg(crypt,L"aes");
    CkCrypt2W_putKeyLength(crypt,128);
    CkCrypt2W_putOaepHash(crypt,L"sha256");
    CkCrypt2W_putOaepPadding(crypt,TRUE);

    // Load the file to be encrypted...
    fileData = CkBinDataW_Create();
    success = CkBinDataW_LoadFile(fileData,L"qa_data/jpg/penguins.jpg");
    // Your app should check for success/failure..

    // Encrypt the data.  The contents of the fileData object are replaced with the PKCS7 encrypted message.
    success = CkCrypt2W_EncryptBd(crypt,fileData);
    if (success != TRUE) {
        wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt));
        CkCrypt2W_Dispose(crypt);
        CkCertW_Dispose(cert1);
        CkCertW_Dispose(cert2);
        CkCertW_Dispose(cert3);
        CkBinDataW_Dispose(fileData);
        return;
    }

    // Save the PKCS7 encrypted message to a file..
    success = CkBinDataW_WriteFile(fileData,L"qa_output/pkcs7_encrypted.p7");

    // Now indicate that the PKCS7 output is to be returned in the base64 encoding.
    CkCrypt2W_putEncodingMode(crypt,L"base64");

    wprintf(L"OK.\n");


    CkCrypt2W_Dispose(crypt);
    CkCertW_Dispose(cert1);
    CkCertW_Dispose(cert2);
    CkCertW_Dispose(cert3);
    CkBinDataW_Dispose(fileData);

    }