Sample code for 30+ languages & platforms
Rust

MIME S/MIME Encryption Algorithm Properties

See more MIME Examples

Demonstrates the properties that control S/MIME encryption: Pkcs7CryptAlg (the symmetric content-encryption algorithm), Pkcs7KeyLength (the content-encryption key length in bits), OaepPadding (whether RSAES-OAEP key transport is used instead of RSAES-PKCS1-v1_5), and the OAEP hash settings OaepHash and OaepMgfHash. The properties are configured before calling Encrypt.

Background. CMS/PKCS #7 encryption uses a symmetric algorithm to protect the content and an RSA key-transport scheme to protect the symmetric key. The defaults (aes, 128-bit, PKCS1-v1_5 padding) work broadly; these properties tune the algorithms and padding.

Chilkat Rust Downloads

Rust

let mime = chilkat::Mime::new();
if mime.set_body_from_plain_text("This message will be encrypted.").is_err() {
    println!("{}", mime.last_error_text());
    return;
}

// Pkcs7CryptAlg is the symmetric content-encryption algorithm.  Supported values are aes, aes-gcm,
// des, 3des, and rc2.  The default is aes.
mime.set_pkcs7_crypt_alg("aes");

// Pkcs7KeyLength is the content-encryption key length in bits.  The default is 128.
mime.set_pkcs7_key_length(256);

// OaepPadding selects the RSA key-transport padding.  The default is false (RSAES-PKCS1-v1_5).
// Set true to use RSAES-OAEP, in which case the OAEP hash settings apply.
mime.set_oaep_padding(true);
mime.set_oaep_hash("sha256");
mime.set_oaep_mgf_hash("sha256");

// Load the recipient certificate (public key is sufficient for encrypting).
let cert = chilkat::Cert::new();
if cert.load_from_file("qa_data/recipient.cer").is_err() {
    println!("{}", cert.last_error_text());
    return;
}

// Encrypt using the algorithm settings configured above.
if mime.encrypt(&cert).is_err() {
    println!("{}", mime.last_error_text());
    return;
}

println!("Encrypted with {} {}-bit key.", mime.pkcs7_crypt_alg(), mime.pkcs7_key_length());