Sample code for 30+ languages & platforms
Rust

Encrypt / Decrypt a File and Verify it has not Changed

See more Encryption Examples

Demonstrates how to encrypt and decrypt a file, and verify it has not changed.

Chilkat Rust Downloads

Rust

// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let crypt = chilkat::Crypt2::new();

crypt.set_crypt_algorithm("aes");
crypt.set_cipher_mode("cbc");
crypt.set_key_length(128);
crypt.set_padding_scheme(0);

let iv_hex = "000102030405060708090A0B0C0D0E0F".to_string();
crypt.set_encoded_iv(&iv_hex, "hex");

let key_hex = "00010203040506071011121314151617".to_string();
crypt.set_encoded_key(&key_hex, "hex");

let data_file = "qa_data/zips/HBIQ040615300005.ZIP".to_string();
let out_file = "qa_output/HBIQ040615300005.enc".to_string();
let out_file2 = "qa_output/HBIQ040615300005.ZIP".to_string();

let _ = crypt.ck_encrypt_file(&data_file, &out_file).is_ok();
let _ = crypt.ck_decrypt_file(&out_file, &out_file2).is_ok();

let fac = chilkat::FileAccess::new();
let b_equal = fac.file_contents_equal(&data_file, &out_file2);
if !b_equal {
    println!("Decrypted file not equal to the original.");
} else {
    println!("Success.");
}