Sample code for 30+ languages & platforms
Zig

Encrypt / Decrypt a File and Verify it has not Changed

See more Encryption Examples

Demonstrates how to encrypt and decrypt a file, and verify it has not changed.

Chilkat Zig Downloads

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

pub fn main() !void {
    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();

    crypt.setCryptAlgorithm("aes");
    crypt.setCipherMode("cbc");
    crypt.setKeyLength(128);
    crypt.setPaddingScheme(0);

    const iv_hex = "000102030405060708090A0B0C0D0E0F";
    crypt.setEncodedIV(iv_hex, "hex");

    const key_hex = "00010203040506071011121314151617";
    crypt.setEncodedKey(key_hex, "hex");

    const data_file = "qa_data/zips/HBIQ040615300005.ZIP";
    const out_file = "qa_output/HBIQ040615300005.enc";
    const out_file2 = "qa_output/HBIQ040615300005.ZIP";

    try crypt.ckEncryptFile(data_file, out_file);
    try crypt.ckDecryptFile(out_file, out_file2);

    const fac = try chilkat.FileAccess.init();
    defer fac.deinit();
    const b_equal = fac.fileContentsEqual(data_file, out_file2);
    if (!b_equal) {
        std.debug.print("Decrypted file not equal to the original.\n", .{});
    } else {
        std.debug.print("Success.\n", .{});
    }
}