Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Duplicate OpensSSL Command that Decrypts Binary DER

See more OpenSSL Examples

This example duplicates the following:
openssl smime -decrypt -in INPUT_FILE -inform der -binary -out OUTPUT_FILE -recip PEM_CERT_AND_KEY -passin pass:PRIVKEY_PASSWORD

Note: Although "smime" is the OpenSSL command, we're not really dealing with S/MIME. The arguments "-inform der -binary" indicate that the input is simply the binary DER (i.e. the PKCS7 binary encrypted object). The output can be any type of file (whatever was encrypted).

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.

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

    crypt.setCryptAlgorithm("pki");

    const pem = try chilkat.Pem.init();
    defer pem.deinit();
    pem.loadPemFile("qa_data/pem/myPem.pem", "password") catch {
        std.debug.print("{s}\n", .{try pem.getLastErrorText(alloc)});
        return;
    };

    const privkey = try chilkat.PrivateKey.init();
    defer privkey.deinit();
    pem.privateKeyAt(0, privkey) catch {
        std.debug.print("{s}\n", .{try pem.getLastErrorText(alloc)});
        return;
    };

    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    pem.certAt(0, cert) catch {
        std.debug.print("{s}\n", .{try pem.getLastErrorText(alloc)});
        return;
    };

    crypt.setDecryptCert2(cert, privkey) catch {
        std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
        return;
    };

    crypt.ckDecryptFile("qa_data/infile.enc", "qa_output/outfile.dat") catch {
        std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
        return;
    };

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