Zig Requires Chilkat v11.0.0+
Zig
RSA Encrypt and OpenSSL Decrypt
See more OpenSSL Examples
Demonstrates how to use Chilkat to RSA encrypt, and then use OpenSSL to decrypt.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 requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const rsa = try chilkat.Rsa.init();
defer rsa.deinit();
const priv_key = try chilkat.PrivateKey.init();
defer priv_key.deinit();
try rsa.genKey(2048, priv_key);
try priv_key.savePkcs8PemFile("qa_output/privKey.pem");
const pub_key = try chilkat.PublicKey.init();
defer pub_key.deinit();
priv_key.toPublicKey(pub_key) catch {};
rsa.setEncodingMode("base64");
const plain_text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890";
const b_use_private_key = false;
rsa.usePublicKey(pub_key) catch {};
const encrypted_str = try rsa.encryptStringENC(alloc, plain_text, b_use_private_key);
const bd = try chilkat.BinData.init();
defer bd.deinit();
bd.appendEncoded(encrypted_str, "base64") catch {};
try bd.writeFile("qa_output/enc.dat");
// The OpenSSL command to decrypt is:
// openssl pkeyutl -in enc.dat -inkey privKey.pem -keyform PEM -pkeyopt rsa_padding_mode:pkcs1 -decrypt
std.debug.print("OK\n", .{});
}