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

RSA Encrypting Symmetric Secret Key

See more RSA Examples

The RSA encryption algorithm is computationally expensive. It is not the best choice for encrypting large amounts of data. Symmetric encryption algorithms such as AES (i.e. Rijndael) or Blowfish are much more efficient. A typical application scenario is that you want to send encrypted messages to a partner, but you don't want to send the symmetric key unprotected. A solution is to generate a public/private RSA key pair and provide your partner with the public key (in advance). You may then encrypt the symmetric algorithm's key using the RSA private key. Next, encrypt the message using the symmetric algorithm, and send your partner both the encrypted key and encrypted message. Your partner decrypts by first RSA decrypting the key, and then uses the decrypted symmetric key to decrypt the message content.

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

    // Load a public/private key pair from a .snk key file.
    // Assume you have already provided your partner with
    // the public key part of the key pair.
    const xml_key = rsa.snkToXml(alloc, "qa_data/rsaKeys/test.snk") catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    const pub_key = try chilkat.PublicKey.init();
    defer pub_key.deinit();
    pub_key.loadFromString(xml_key) catch {
        std.debug.print("{s}\n", .{try pub_key.getLastErrorText(alloc)});
        return;
    };

    rsa.usePublicKey(pub_key) catch {};

    // Our message data will be encrypted using 128-bit AES
    // encryption, using CBC (cipher-block chaining).
    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();
    crypt.setCryptAlgorithm("aes");
    crypt.setCipherMode("cbc");
    crypt.setKeyLength(128);

    // Generate 128-bit (16 bytes) secret key and return as a hex string.
    crypt.setEncodingMode("hex");
    const secret_key = try crypt.genRandomBytesENC(alloc, 16);
    std.debug.print("Unencrypted Key: {s}\n", .{secret_key});

    // Use the key we generated:
    crypt.setEncodedKey(secret_key, "hex");

    // RSA encrypt the secret key and return as a hex string:
    rsa.setEncodingMode("hex");
    var b_use_private_key: bool = false;
    const encrypted_key = try rsa.encryptStringENC(alloc, secret_key, b_use_private_key);

    // Symmetric encrypt a message.  For this example the message
    // is very short, but typically this is where a large amount
    // of data may be encrypted.
    crypt.setEncodingMode("base64");
    const encrypted_text = try crypt.encryptStringENC(alloc, "Hello World!");

    // Show our encrypted key and encrypted text:
    std.debug.print("Encrypted Key: {s}\n", .{encrypted_key});
    std.debug.print("Encrypted Text: {s}\n", .{encrypted_text});

    // Assume we sent these strings to our partner...

    // Here's what we do at the partner end:
    const priv_key = try chilkat.PrivateKey.init();
    defer priv_key.deinit();
    priv_key.loadXml(xml_key) catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

    rsa.usePrivateKey(priv_key) catch {};

    // First, decrypt the encryptedKey:
    b_use_private_key = true;
    const decrypted_key = try rsa.decryptStringENC(alloc, encrypted_key, b_use_private_key);
    std.debug.print("Decrypted Key: {s}\n", .{decrypted_key});

    // Set our crypt object's properties and secret key:
    const crypt2 = try chilkat.Crypt2.init();
    defer crypt2.deinit();
    crypt2.setCryptAlgorithm("aes");
    crypt2.setCipherMode("cbc");
    crypt2.setKeyLength(128);
    crypt2.setEncodingMode("base64");
    crypt2.setEncodedKey(decrypted_key, "hex");

    // Decrypt the message:
    const decrypted_text = try crypt2.decryptStringENC(alloc, encrypted_text);
    std.debug.print("Decrypted Text: {s}\n", .{decrypted_text});
}