Zig
Zig
S/MIME Encrypt .eml without Sending
See more Email Object Examples
Demonstrates how to encrypt an email using the recipient's digital certificate. This example just encrypts, and does not send the email.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 requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const email = try chilkat.Email.init();
defer email.deinit();
email.loadEml("c:/temp/email/unencrypted.eml") catch {
std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
return;
};
// The email content is encrypted using AES with a 256-bit key, operating in GCM mode, which provides authenticated encryption.
email.setPkcs7CryptAlg("aes-gcm");
email.setPkcs7KeyLength(256);
email.setOaepPadding(true);
email.setOaepHash("sha256");
email.setOaepMgfHash("sha256");
const cert = try chilkat.Cert.init();
defer cert.deinit();
cert.loadFromFile("c/temps/cert/recipient.cer") catch {
std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
return;
};
email.setSendEncrypted(true);
email.setEncryptCert(cert) catch {};
const sb_smime = try chilkat.StringBuilder.init();
defer sb_smime.deinit();
// The mailman object applies the encryption by rendering the email according to the instructions (property settings) provided in the email object.
// No email is sent.
const mailman = try chilkat.MailMan.init();
defer mailman.deinit();
mailman.renderToMimeSb(email, sb_smime) catch {
std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
return;
};
sb_smime.writeFile("c:/temp/encryptedEmail.eml", "utf-8", false) catch {
std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
return;
};
std.debug.print("Success!\n", .{});
}