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

Generate RSA Key and return Base64 PKCS8 Private Key

See more RSA Examples

Demonstrates how to generate a new 2048-bit RSA private key and returns the Base64 encoded PKCS8 representation of the private key.

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

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

    // Get the private key in PKCS8 Base64 format
    const priv_key_pkcs8_base64 = try priv_key.getPkcs8ENC(alloc, "base64");

    // The key in base64 format will start similar to this:
    // MIIEvAIBADANBgkqhkiG9w0BA...
    std.debug.print("{s}\n", .{priv_key_pkcs8_base64});
}