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

RSA OAEP Padding

See more RSA Examples

Demonstrates how to use OAEP padding with the RSA encryption algorithm. More information about OAEP Padding.

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();

    // 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();

    // RSA public key in XML format:
    const pub_key_xml = "<RSAPublicKey><Modulus>of3im3mRgd2NLXIGoK6uYLg6jj0Ug2b42rnqa5Tbwz2ieFqMJqt+++x2oqLYGurlz49nt+7/785g3XYWqoka4u9c9zul6YubIjnBM72dQy7rEkEfbUxgjcxqXyjZFx+FpaxFUecLu688XEu+9UA42VKiCgcl+E7TrqnfeeYpNXc=</Modulus><Exponent>AQAB</Exponent></RSAPublicKey>";

    const pub_key = try chilkat.PublicKey.init();
    defer pub_key.deinit();
    pub_key.loadFromString(pub_key_xml) 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;
    };

    // To use Optimal Asymmetric Encryption Padding (OAEP) padding,
    // simply set the PkcsPadding property to false
    rsa.setPkcsPadding(false);

    // Encrypt a string and return the encrypted data base64-encoded:
    rsa.setEncodingMode("base64");

    const plain_text = "RSA Encryption should be easy.";

    const use_private_key = false;
    const encrypted_str = try rsa.encryptStringENC(alloc, plain_text, use_private_key);
    std.debug.print("{s}\n", .{encrypted_str});
}