Sample code for 30+ languages & platforms
Zig

Example: Crypt2.EncryptEncoded method

Demonstrates how to call the EncryptEncoded method.

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();

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

    crypt.setCryptAlgorithm("aes");
    crypt.setCipherMode("cbc");
    crypt.setKeyLength(128);
    crypt.setEncodedKey("000102030405060708090A0B0C0D0E0F", "hex");
    crypt.setEncodedIV("000102030405060708090A0B0C0D0E0F", "hex");

    // Encrypt the bytes 0x00, 0x01, 0x02, ... 0x0A
    // and return the encrypted bytes using the lowercase hex encoding.
    crypt.setEncodingMode("hex_lower");
    const encrypted = try crypt.encryptEncoded(alloc, "000102030405060708090a");
    std.debug.print("{s}\n", .{encrypted});

    // Output:
    // 9da2ae71a5378487114b430e5e230378

    const decrypted = try crypt.decryptEncoded(alloc, encrypted);
    std.debug.print("{s}\n", .{decrypted});

    // Output:
    // 000102030405060708090a
}