Zig Requires Chilkat v11.0.0+
Zig
Convert RSA Private Key to Public Key
See more RSA Examples
Demonstrates how to get a public RSA key from a private RSA key.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const priv_key = try chilkat.PrivateKey.init();
defer priv_key.deinit();
// Step 1: Load the private key from a source.
// (Chilkat can load private keys from all types of formats, and from in-memory bytes or encoded strings.
// see the online reference documentation for more options.)
priv_key.loadPemFile("qa_data/pem/VP_Private.pem") catch {
std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
return;
};
// Step 2: Get the public key object from the private key object.
const pub_key = try chilkat.PublicKey.init();
defer pub_key.deinit();
priv_key.toPublicKey(pub_key) catch {};
// Step 3: Save the public key in a desired format.
// (Chilkat can load or save public and private keys in many different formats. See
// the online reference documentation for more options.)
// Saves to a PKCS8 PEM file.
const b_prefer_pkcs1 = false;
pub_key.savePemFile(b_prefer_pkcs1, "qa_data/pem/VP_Public.pem") catch {
std.debug.print("{s}\n", .{try pub_key.getLastErrorText(alloc)});
return;
};
std.debug.print("Extracted and saved public key from private key.\n", .{});
}