Sample code for 30+ languages & platforms
Zig 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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // 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

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();
    const info = try chilkat.JsonObject.init();
    defer info.deinit();

    // AES-256, the default.  Saved as PDF 1.7 with Adobe extension level 8.
    const pdf1 = try chilkat.Pdf.init();
    defer pdf1.deinit();
    pdf1.loadFile("qa_data/pdf/two_page_memo.pdf") catch {
        std.debug.print("{s}\n", .{try pdf1.getLastErrorText(alloc)});
        return;
    };

    pdf1.setUserPassword("user-secret");
    pdf1.setOwnerPassword("owner-secret");
    json.updateString("algorithmChoice", "aes-256") catch {};
    pdf1.protection("encrypt", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf1.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("aes-256 will be saved as PDF {s}, Adobe extension level {d}\n", .{ try json_out.stringOf(alloc, "pending.pdfVersion"), json_out.intOf("pending.adobeExtensionLevel") });
    pdf1.writeFile("qa_output/memo_aes256.pdf") catch {
        std.debug.print("{s}\n", .{try pdf1.getLastErrorText(alloc)});
        return;
    };

    pdf1.preLoadCheck("qa_output/memo_aes256.pdf", info) catch {
        std.debug.print("{s}\n", .{try pdf1.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("  saved file: {s}, PDF {s}\n", .{ try info.stringOf(alloc, "algorithm"), try info.stringOf(alloc, "pdfVersion") });

    // AES-128, for readers that predate AES-256.  Saved as PDF 1.6.
    const pdf2 = try chilkat.Pdf.init();
    defer pdf2.deinit();
    pdf2.loadFile("qa_data/pdf/two_page_memo.pdf") catch {
        std.debug.print("{s}\n", .{try pdf2.getLastErrorText(alloc)});
        return;
    };

    pdf2.setUserPassword("user-secret");
    pdf2.setOwnerPassword("owner-secret");
    json.updateString("algorithmChoice", "aes-128") catch {};
    pdf2.protection("encrypt", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf2.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("aes-128 will be saved as PDF {s}\n", .{try json_out.stringOf(alloc, "pending.pdfVersion")});
    pdf2.writeFile("qa_output/memo_aes128.pdf") catch {
        std.debug.print("{s}\n", .{try pdf2.getLastErrorText(alloc)});
        return;
    };

    pdf2.preLoadCheck("qa_output/memo_aes128.pdf", info) catch {
        std.debug.print("{s}\n", .{try pdf2.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("  saved file: {s}, PDF {s}\n", .{ try info.stringOf(alloc, "algorithm"), try info.stringOf(alloc, "pdfVersion") });

    // RC4-128, a broken cipher, only for readers too old for AES.  Saved as PDF 1.4.
    const pdf3 = try chilkat.Pdf.init();
    defer pdf3.deinit();
    pdf3.loadFile("qa_data/pdf/two_page_memo.pdf") catch {
        std.debug.print("{s}\n", .{try pdf3.getLastErrorText(alloc)});
        return;
    };

    pdf3.setUserPassword("user-secret");
    pdf3.setOwnerPassword("owner-secret");
    json.updateString("algorithmChoice", "rc4-128") catch {};
    pdf3.protection("encrypt", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf3.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("rc4-128 will be saved as PDF {s}\n", .{try json_out.stringOf(alloc, "pending.pdfVersion")});
    pdf3.writeFile("qa_output/memo_rc4_128.pdf") catch {
        std.debug.print("{s}\n", .{try pdf3.getLastErrorText(alloc)});
        return;
    };

    pdf3.preLoadCheck("qa_output/memo_rc4_128.pdf", info) catch {
        std.debug.print("{s}\n", .{try pdf3.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("  saved file: {s}, PDF {s}\n", .{ try info.stringOf(alloc, "algorithm"), try info.stringOf(alloc, "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
}