Zig
Zig
Encrypt MIME for Multiple Recipients
See more MIME Examples
Demonstrates encrypting the current MIME entity for several recipients at once. A certificate is added for each recipient with AddEncryptCert, and EncryptN then produces a single CMS/PKCS #7 message that each recipient can independently decrypt.
The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Background. When a message targets more than one recipient, each recipient's certificate is added to the internal list and EncryptN wraps the message key separately for each one. Any single recipient can then decrypt with their own private key.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const mime = try chilkat.Mime.init();
defer mime.deinit();
mime.setBodyFromPlainText("This message will be encrypted.") catch {
std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
return;
};
// Add a certificate for each intended recipient. Every recipient receives an independently
// wrapped copy of the message key.
const cert1 = try chilkat.Cert.init();
defer cert1.deinit();
cert1.loadFromFile("qa_data/recipient1.cer") catch {
std.debug.print("{s}\n", .{try cert1.getLastErrorText(alloc)});
return;
};
mime.addEncryptCert(cert1) catch {
std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
return;
};
const cert2 = try chilkat.Cert.init();
defer cert2.deinit();
cert2.loadFromFile("qa_data/recipient2.cer") catch {
std.debug.print("{s}\n", .{try cert2.getLastErrorText(alloc)});
return;
};
mime.addEncryptCert(cert2) catch {
std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
return;
};
// Encrypt for every added recipient at once.
mime.encryptN() catch {
std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
return;
};
mime.saveMime("qa_output/encrypted_multi.eml") catch {
std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
return;
};
std.debug.print("Message encrypted for multiple recipients.\n", .{});
}