Sample code for 30+ languages & platforms
Rust

Encrypt / Decrypt Secure Strings

See more Encryption Examples

Demonstrates how to use the EncryptSecureENC and DecryptSecureENC methods to encrypt/decrypt secure strings. These methods were added in Chilkat v9.5.0.71 (released January 2018).

Chilkat Rust Downloads

Rust

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

// Load the secure string with some text.
let sec_str1 = chilkat::SecureString::new();
if sec_str1.load_file("qa_data/txt/helloWorld.txt", "utf-8").is_err() {
    println!("Failed to load helloWorld.txt");
    return;
}

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

crypt.set_crypt_algorithm("aes");
crypt.set_cipher_mode("cbc");
crypt.set_key_length(128);
crypt.set_encoded_key("000102030405060708090A0B0C0D0E0F", "hex");
crypt.set_encoded_iv("000102030405060708090A0B0C0D0E0F", "hex");
crypt.set_encoding_mode("base64");

// Return the base64 encoded encrypted contents of secStr1.
let encrypted_str = crypt.encrypt_secure_enc(&sec_str1).unwrap_or_default();
println!("Encrypted string: {}", encrypted_str);

// Output:
// Encrypted string: qiq+IFhcjTkEIkZyf31V/g==

// Decrypt to secStr2:
let sec_str2 = chilkat::SecureString::new();
let _ = crypt.decrypt_secure_enc(&encrypted_str, &sec_str2);

// Access the contents of secStr2
println!("Decrypted string: {}", sec_str2.access().unwrap_or_default());

// Output:
// Decrypted string: Hello World!