Zig Requires Chilkat v11.0.0+
Zig
Generate RSA Public/Private Key Pair and Export to PEM
See more RSA Examples
_LANGUAGE_ example code showing how to generate an RSA public/private key pair and export to PEM files.Chilkat Zig Downloads
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 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;
};
const pub_key = try chilkat.PublicKey.init();
defer pub_key.deinit();
priv_key.toPublicKey(pub_key) catch {};
// Save the private key in PEM format:
priv_key.savePemFile("privateKey.pem") catch {
std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
return;
};
// Save the public key in PEM format:
pub_key.savePemFile(false, "publicKey.pem") catch {
std.debug.print("{s}\n", .{try pub_key.getLastErrorText(alloc)});
return;
};
std.debug.print("Success.\n", .{});
}