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

Choose the PDF Encryption Algorithm: AES-256, AES-128 or RC4

See more PDF Protection Examples

Demonstrates the algorithmChoice option of the encrypt operation. aes-256 is the default and the one to use unless an older reader must open the file. aes-128 is for readers that predate AES-256. rc4-128 and rc4-40 are broken ciphers kept only for very old readers. Each algorithm needs a minimum PDF version, and the file header is raised to it when the PDF is saved, never lowered. A PDF saved with RC4 is written with a classic cross-reference table so that those old readers can parse it.

Chilkat C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObject json;
    HCkJsonObject jsonOut;
    HCkJsonObject info;
    HCkPdf pdf1;
    HCkPdf pdf2;
    HCkPdf pdf3;

    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

    json = CkJsonObject_Create();
    jsonOut = CkJsonObject_Create();
    info = CkJsonObject_Create();

    //  AES-256, the default.  Saved as PDF 1.7 with Adobe extension level 8.
    pdf1 = CkPdf_Create();
    success = CkPdf_LoadFile(pdf1,"qa_data/pdf/two_page_memo.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf1));
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkJsonObject_Dispose(info);
        CkPdf_Dispose(pdf1);
        return;
    }

    CkPdf_putUserPassword(pdf1,"user-secret");
    CkPdf_putOwnerPassword(pdf1,"owner-secret");
    CkJsonObject_UpdateString(json,"algorithmChoice","aes-256");
    success = CkPdf_Protection(pdf1,"encrypt",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf1));
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkJsonObject_Dispose(info);
        CkPdf_Dispose(pdf1);
        return;
    }

    printf("aes-256 will be saved as PDF %s, Adobe extension level %d\n",CkJsonObject_stringOf(jsonOut,"pending.pdfVersion"),CkJsonObject_IntOf(jsonOut,"pending.adobeExtensionLevel"));
    success = CkPdf_WriteFile(pdf1,"qa_output/memo_aes256.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf1));
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkJsonObject_Dispose(info);
        CkPdf_Dispose(pdf1);
        return;
    }

    success = CkPdf_PreLoadCheck(pdf1,"qa_output/memo_aes256.pdf",info);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf1));
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkJsonObject_Dispose(info);
        CkPdf_Dispose(pdf1);
        return;
    }

    printf("  saved file: %s, PDF %s\n",CkJsonObject_stringOf(info,"algorithm"),CkJsonObject_stringOf(info,"pdfVersion"));

    //  AES-128, for readers that predate AES-256.  Saved as PDF 1.6.
    pdf2 = CkPdf_Create();
    success = CkPdf_LoadFile(pdf2,"qa_data/pdf/two_page_memo.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf2));
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkJsonObject_Dispose(info);
        CkPdf_Dispose(pdf1);
        CkPdf_Dispose(pdf2);
        return;
    }

    CkPdf_putUserPassword(pdf2,"user-secret");
    CkPdf_putOwnerPassword(pdf2,"owner-secret");
    CkJsonObject_UpdateString(json,"algorithmChoice","aes-128");
    success = CkPdf_Protection(pdf2,"encrypt",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf2));
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkJsonObject_Dispose(info);
        CkPdf_Dispose(pdf1);
        CkPdf_Dispose(pdf2);
        return;
    }

    printf("aes-128 will be saved as PDF %s\n",CkJsonObject_stringOf(jsonOut,"pending.pdfVersion"));
    success = CkPdf_WriteFile(pdf2,"qa_output/memo_aes128.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf2));
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkJsonObject_Dispose(info);
        CkPdf_Dispose(pdf1);
        CkPdf_Dispose(pdf2);
        return;
    }

    success = CkPdf_PreLoadCheck(pdf2,"qa_output/memo_aes128.pdf",info);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf2));
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkJsonObject_Dispose(info);
        CkPdf_Dispose(pdf1);
        CkPdf_Dispose(pdf2);
        return;
    }

    printf("  saved file: %s, PDF %s\n",CkJsonObject_stringOf(info,"algorithm"),CkJsonObject_stringOf(info,"pdfVersion"));

    //  RC4-128, a broken cipher, only for readers too old for AES.  Saved as PDF 1.4.
    pdf3 = CkPdf_Create();
    success = CkPdf_LoadFile(pdf3,"qa_data/pdf/two_page_memo.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf3));
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkJsonObject_Dispose(info);
        CkPdf_Dispose(pdf1);
        CkPdf_Dispose(pdf2);
        CkPdf_Dispose(pdf3);
        return;
    }

    CkPdf_putUserPassword(pdf3,"user-secret");
    CkPdf_putOwnerPassword(pdf3,"owner-secret");
    CkJsonObject_UpdateString(json,"algorithmChoice","rc4-128");
    success = CkPdf_Protection(pdf3,"encrypt",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf3));
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkJsonObject_Dispose(info);
        CkPdf_Dispose(pdf1);
        CkPdf_Dispose(pdf2);
        CkPdf_Dispose(pdf3);
        return;
    }

    printf("rc4-128 will be saved as PDF %s\n",CkJsonObject_stringOf(jsonOut,"pending.pdfVersion"));
    success = CkPdf_WriteFile(pdf3,"qa_output/memo_rc4_128.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf3));
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkJsonObject_Dispose(info);
        CkPdf_Dispose(pdf1);
        CkPdf_Dispose(pdf2);
        CkPdf_Dispose(pdf3);
        return;
    }

    success = CkPdf_PreLoadCheck(pdf3,"qa_output/memo_rc4_128.pdf",info);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf3));
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkJsonObject_Dispose(info);
        CkPdf_Dispose(pdf1);
        CkPdf_Dispose(pdf2);
        CkPdf_Dispose(pdf3);
        return;
    }

    printf("  saved file: %s, PDF %s\n",CkJsonObject_stringOf(info,"algorithm"),CkJsonObject_stringOf(info,"pdfVersion"));

    //  Sample output:

    //  aes-256 will be saved as PDF 1.7, Adobe extension level 8
    //    saved file: aes-256, PDF 1.7
    //  aes-128 will be saved as PDF 1.6
    //    saved file: aes-128, PDF 1.6
    //  rc4-128 will be saved as PDF 1.4
    //    saved file: rc4-128, PDF 1.4


    CkJsonObject_Dispose(json);
    CkJsonObject_Dispose(jsonOut);
    CkJsonObject_Dispose(info);
    CkPdf_Dispose(pdf1);
    CkPdf_Dispose(pdf2);
    CkPdf_Dispose(pdf3);

    }