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

Encrypt a PDF for a Certificate Instead of a Password

See more PDF Protection Examples

Demonstrates certificate security, where a PDF is encrypted for one or more recipients and each opens it with their own certificate and private key. No password is involved. Only the public certificate of a recipient is needed to encrypt. Each recipient has its own permissions, and the changeSecurity permission gives a recipient owner rights. PreLoadCheck tells an application that a file is certificate-encrypted and which certificates can open it. A PDF that cannot be opened, because no matching certificate and private key were provided, fails to load.

Chilkat C Downloads

C
#include <C_CkCert.h>
#include <C_CkPdf.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkCert recipient;
    HCkPdf pdf;
    HCkJsonObject json;
    HCkJsonObject jsonOut;
    HCkPdf reader;
    HCkJsonObject info;
    HCkCert myCert;
    HCkJsonObject empty;
    HCkJsonObject loadedInfo;

    success = FALSE;

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

    //  The PDF used in this example is available at two_page_memo.pdf

    //  The recipient's certificate.  Only the public certificate is needed to encrypt.
    recipient = CkCert_Create();
    success = CkCert_LoadFromFile(recipient,"qa_data/certs/recipient.cer");
    if (success == FALSE) {
        printf("%s\n",CkCert_lastErrorText(recipient));
        CkCert_Dispose(recipient);
        return;
    }

    pdf = CkPdf_Create();
    success = CkPdf_LoadFile(pdf,"qa_data/pdf/two_page_memo.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkCert_Dispose(recipient);
        CkPdf_Dispose(pdf);
        return;
    }

    //  Call AddEncryptCert once for each recipient.
    success = CkPdf_AddEncryptCert(pdf,recipient);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkCert_Dispose(recipient);
        CkPdf_Dispose(pdf);
        return;
    }

    //  recipientPermissions[0] belongs to the first certificate added.  changeSecurity gives that
    //  recipient owner rights, so the file can be managed later.
    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"method","certificate");
    CkJsonObject_UpdateBool(json,"recipientPermissions[0].changeSecurity",TRUE);

    jsonOut = CkJsonObject_Create();
    success = CkPdf_Protection(pdf,"encrypt",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkCert_Dispose(recipient);
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    printf("method: %s\n",CkJsonObject_stringOf(jsonOut,"pending.method"));
    printf("algorithm: %s\n",CkJsonObject_stringOf(jsonOut,"pending.algorithm"));
    printf("recipients: %d\n",CkJsonObject_SizeOfArray(jsonOut,"pending.recipients"));

    success = CkPdf_WriteFile(pdf,"qa_output/memo_for_recipient.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkCert_Dispose(recipient);
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    //  PreLoadCheck says how the file is protected, without loading it.
    reader = CkPdf_Create();
    info = CkJsonObject_Create();
    success = CkPdf_PreLoadCheck(reader,"qa_output/memo_for_recipient.pdf",info);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(reader));
        CkCert_Dispose(recipient);
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(reader);
        CkJsonObject_Dispose(info);
        return;
    }

    printf("saved file, method: %s\n",CkJsonObject_stringOf(info,"method"));
    printf("saved file, recipients: %d\n",CkJsonObject_SizeOfArray(info,"recipients"));

    //  The recipient opens it with the certificate and its private key, here from a .pfx file.
    //  (On Windows, a certificate that is installed in the Windows certificate store with its private
    //  key is found automatically when the PDF is loaded, and SetDecryptCert is not needed for it.)
    myCert = CkCert_Create();
    success = CkCert_LoadPfxFile(myCert,"qa_data/pfx/recipient.pfx","pfx-password");
    if (success == FALSE) {
        printf("%s\n",CkCert_lastErrorText(myCert));
        CkCert_Dispose(recipient);
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(reader);
        CkJsonObject_Dispose(info);
        CkCert_Dispose(myCert);
        return;
    }

    success = CkPdf_SetDecryptCert(reader,myCert);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(reader));
        CkCert_Dispose(recipient);
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(reader);
        CkJsonObject_Dispose(info);
        CkCert_Dispose(myCert);
        return;
    }

    success = CkPdf_LoadFile(reader,"qa_output/memo_for_recipient.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(reader));
        CkCert_Dispose(recipient);
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(reader);
        CkJsonObject_Dispose(info);
        CkCert_Dispose(myCert);
        return;
    }

    empty = CkJsonObject_Create();
    loadedInfo = CkJsonObject_Create();
    success = CkPdf_Protection(reader,"getInfo",empty,loadedInfo);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(reader));
        CkCert_Dispose(recipient);
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(reader);
        CkJsonObject_Dispose(info);
        CkCert_Dispose(myCert);
        CkJsonObject_Dispose(empty);
        CkJsonObject_Dispose(loadedInfo);
        return;
    }

    printf("opened with: %s\n",CkJsonObject_stringOf(loadedInfo,"loaded.openedWith"));
    printf("pages: %d\n",CkPdf_getNumPages(reader));

    //  Sample output:

    //  method: certificate
    //  algorithm: aes-256
    //  recipients: 1
    //  saved file, method: certificate
    //  saved file, recipients: 1
    //  opened with: certificate
    //  pages: 2


    CkCert_Dispose(recipient);
    CkPdf_Dispose(pdf);
    CkJsonObject_Dispose(json);
    CkJsonObject_Dispose(jsonOut);
    CkPdf_Dispose(reader);
    CkJsonObject_Dispose(info);
    CkCert_Dispose(myCert);
    CkJsonObject_Dispose(empty);
    CkJsonObject_Dispose(loadedInfo);

    }