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

Generate RSA Key and Export to Encrypted PEM

See more RSA Examples

_LANGUAGE_ sample showing how to generate an RSA key and export to encrypted PEM.

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 rsa = try chilkat.Rsa.init();
    defer rsa.deinit();

    // Generate a 1024-bit key.  Chilkat RSA supports
    // key sizes ranging from 512 bits to 4096 bits.
    const priv_key = try chilkat.PrivateKey.init();
    defer priv_key.deinit();
    rsa.genKey(1024, priv_key) catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    const str_enc_pem = try priv_key.getPkcs8EncryptedPem(alloc, "myPassword");

    // You now have the private key in an encrypted format,
    // which may be (for example) saved to a database.

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