Sample code for 30+ languages & platforms
Zig

Extract PKCS7 from MIME and Decrypt

See more MIME Examples

Extracts the base64-encoded PKCS7 body of a MIME message to a file, and then decrypts using Chilkat Crypt2.

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 assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const mime = try chilkat.Mime.init();
    defer mime.deinit();

    mime.loadMimeFile("c:/aaworkarea/EmailInBytes.txt") catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    mime.saveBody("c:/aaworkarea/smime.p7m") catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

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

    crypt.addPfxSourceFile("c:/aaworkarea/my.pfx", "pfxPassword") catch {
        std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
        return;
    };

    // Indicate the public-key (PKCS7) encryption/decryption should be used:
    crypt.setCryptAlgorithm("pki");

    const in_path = "c:/aaworkarea/smime.p7m";
    const out_path = "c:/aaworkarea/decrypted.dat";

    crypt.ckDecryptFile(in_path, out_path) catch {
        std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Success.\n", .{});
}