Rust
Rust
AES Encrypt String (utf-8 byte representation) and return Base64
See more Encryption Examples
Demonstrates how to AES encrypt the utf-8 byte representation of a string and return in base64 format.Chilkat Rust Downloads
// This example 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(256);
crypt.set_charset("utf-8");
crypt.set_encoding_mode("base64");
let iv_hex = "000102030405060708090A0B0C0D0E0F".to_string();
crypt.set_encoded_iv(&iv_hex, "hex");
let key_hex = "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F".to_string();
crypt.set_encoded_key(&key_hex, "hex");
let encrypted_str = crypt.encrypt_string_enc("This is the original string").unwrap_or_default();
println!("{}", encrypted_str);
// Let's say we want to URL encode the base64 string..
crypt.set_crypt_algorithm("none");
crypt.set_encoding_mode("url");
let url_encoded = crypt.encrypt_string_enc(&encrypted_str).unwrap_or_default();
println!("{}", url_encoded);
// Sample output:
// q3fmgEqjqa9o//ZS6aPuh4Wtbrrxx/WOIQSKeVnesZg=
// q3fmgEqjqa9o%2F%2FZS6aPuh4Wtbrrxx%2FWOIQSKeVnesZg%3D
// -------------------------------------------------------------------
// Now let's do the reverse...
//
// We start with a string that is the binary encrypted data, base64 encoded, and then URL encoded.
let enc_base64_url = "q3fmgEqjqa9o%2F%2FZS6aPuh4Wtbrrxx%2FWOIQSKeVnesZg%3D".to_string();
// We'll use the same crypt object. If using a new instance of the crypt object,
// make sure *all* settings are identical: the algorithm, cipher mode, key length, charset, encoding mode, IV, and secret key.
// Decode from URL...
crypt.set_crypt_algorithm("none");
crypt.set_encoding_mode("url");
let enc_base64 = crypt.decrypt_string_enc(&url_encoded).unwrap_or_default();
println!("{}", enc_base64);
// Now decrypt...
crypt.set_crypt_algorithm("aes");
crypt.set_encoding_mode("base64");
let original_str = crypt.decrypt_string_enc(&enc_base64).unwrap_or_default();
println!("{}", original_str);