Zig Requires Chilkat v11.0.0+
Zig
RSA Encrypt with Modulus and Exponent
See more RSA Examples
Demonstrates how to RSA encrypt with a given modulus and exponent.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();
// Assuming you already have a base64 modulus and exponent,
// wrap it in XML like this:
const modulus = "qMBRpdYrAy5aMmo31NErUizh5sbweguSmh4wlK6uJEIDl+kwTlROnE34KOFExeTbJSX0WygPi+vWl0yNq7buIMUKpytossAAWut5khO3CQJxTk7G2gnEPNUUXHiExGgNrLzcSLv8YIlfVALhoRWyC67KOL+a+3taNq3h+BHeWhM=";
const exponent = "AQAB";
const xml = try chilkat.Xml.init();
defer xml.deinit();
xml.setTag("RSAPublicKey");
xml.newChild2("Modulus", modulus);
xml.newChild2("Exponent", exponent);
const pub_key = try chilkat.PublicKey.init();
defer pub_key.deinit();
pub_key.loadFromString(try xml.getXml(alloc)) catch {
std.debug.print("{s}\n", .{try pub_key.getLastErrorText(alloc)});
return;
};
rsa.usePublicKey(pub_key) catch {
std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
return;
};
const use_private_key = false;
const plain_text = "message in a bottle";
rsa.setEncodingMode("base64");
const encrypted_str_base64 = try rsa.encryptStringENC(alloc, plain_text, use_private_key);
std.debug.print("{s}\n", .{encrypted_str_base64});
}