Zig Requires Chilkat v11.0.0+
Zig
RSA Encrypt/Decrypt AES Key
See more RSA Examples
Demonstrates how to use RSA to protect a key for AES encryption. It can be used in this scenario: You will provide your RSA public key to any number of counterparts. Your counterpart will generate an AES key, encrypt data (or a file) using it, then encrypt the AES key using your RSA public key. Your counterpart sends you both the encrypted data and the encrypted key. Since you are the only one with access to the RSA private key, only you can decrypt the AES key. You decrypt the key, then decrypt the data using the AES key.This example will show the entire process. (1) Generate an RSA key and save both private and public parts to PEM files. (2) Encrypt a file using a randomly generated AES encryption key. (3) RSA encrypt the AES key. (4) RSA decrypt the AES key. (5) Use it to AES decrypt the file or data.
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.
// ------------------
// Step 1. Generate an RSA key and save to PEM files.
const rsa = try chilkat.Rsa.init();
defer rsa.deinit();
// Generate a 2048-bit key.
const priv_key = try chilkat.PrivateKey.init();
defer priv_key.deinit();
rsa.genKey(2048, priv_key) catch {
std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
return;
};
const pub_key = try chilkat.PublicKey.init();
defer pub_key.deinit();
priv_key.toPublicKey(pub_key) catch {};
// For brevity, we are not checking the return value...
try pub_key.savePemFile(false, "qa_temp/pubKey.pem");
try priv_key.savePemFile("qa_temp/privKey.pem");
// Other methods exist for saving the private key in PKCS8 format,
// both encrypted and un-encrypted..
// ------------------
// Step 2. This is the code your counterpart will run to
// AES encrypt a file. It generates a random AES key
// to use for the encryption.
const crypt = try chilkat.Crypt2.init();
defer crypt.deinit();
crypt.setCryptAlgorithm("aes");
crypt.setCipherMode("cbc");
crypt.setKeyLength(256);
// Generate random bytes and return the random bytes
// in a hex string:
crypt.setEncodingMode("hex");
const random_key = try crypt.genRandomBytesENC(alloc, 32);
std.debug.print("AES key = {s}\n", .{random_key});
// Set the key.
crypt.setEncodedKey(random_key, "hex");
// Set the IV to a known value that will be used on both sides.
// (If desired, you could generate a random IV and protect it in the same
// way as the key...)
// The length of the IV for AES is always 16 bytes, regardless of the key size.
crypt.setEncodedIV("000102030405060708090A0B0C0D0E0F", "hex");
// AES Encrypt the file (the file may be any size because it will
// stream the file in/out.
crypt.ckEncryptFile("qa_data/hamlet.xml", "qa_temp/aesEncrypted.dat") catch {
std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
return;
};
// ------------------
// Step 3.
// ------------------
// RSA encrypt the AES key.
// We'll pretend your counter part has the public-key PEM file and needs
// to load it. Therefore, we'll start with a new RSA object and load
// the public key from the PEM..
const rsa2 = try chilkat.Rsa.init();
defer rsa2.deinit();
const pub_key2 = try chilkat.PublicKey.init();
defer pub_key2.deinit();
try pub_key2.loadFromFile("qa_temp/pubKey.pem");
try rsa2.usePublicKey(pub_key2);
// RSA encrypt the AES key and return it as a base64 encoded string.
rsa2.setEncodingMode("base64");
var b_use_private_key: bool = false;
const encrypted_aes_key = try rsa2.encryptStringENC(alloc, random_key, b_use_private_key);
// At this point, your counterpart sends you the encryptedAesKey,
// and the encrypted file.
// ------------------
// Step 4 - RSA decrypt the AES key.
// ------------------
// Start with a new RSA object and load the private key from the PEM.
const rsa3 = try chilkat.Rsa.init();
defer rsa3.deinit();
const priv_key2 = try chilkat.PrivateKey.init();
defer priv_key2.deinit();
try priv_key2.loadPemFile("qa_temp/privKey.pem");
try rsa3.usePrivateKey(priv_key2);
// The encrypted AES key is encoded using base64, so set
// our EncodingMode to "base64".
rsa3.setEncodingMode("base64");
b_use_private_key = true;
const decrypted_aes_key = try rsa3.decryptStringENC(alloc, encrypted_aes_key, b_use_private_key);
std.debug.print("decryptedAesKey = {s}\n", .{decrypted_aes_key});
// ------------------
// Step 5 - AES decrypt the file.
// ------------------
const crypt2 = try chilkat.Crypt2.init();
defer crypt2.deinit();
crypt2.setCryptAlgorithm("aes");
crypt2.setCipherMode("cbc");
crypt2.setKeyLength(256);
// Set the key.
crypt2.setEncodedKey(decrypted_aes_key, "hex");
// Set the IV
crypt2.setEncodedIV("000102030405060708090A0B0C0D0E0F", "hex");
// AES Decrypt the file (the file may be any size because it will
// stream the file in/out.
crypt2.ckDecryptFile("qa_temp/aesEncrypted.dat", "qa_temp/decrypted.xml") catch {
std.debug.print("{s}\n", .{try crypt2.getLastErrorText(alloc)});
return;
};
}