Rust Requires Chilkat v11.0.0+
Rust
RSA Encrypt RSA/ECB/OAEPWithSHA1AndMGF1Padding
See more RSA Examples
Demonstrates how to RSA encrypt using RSA/ECB/OAEPWithSHA1AndMGF1Padding. Also demonstrates RSA/ECB/OAEPWithSHA-256AndMGF1Padding. Both of these terms are from Java's JCE. Note: In this context, "ECB" doesn't actually mean anything. It's a symmetric cipher mode that doesn't apply (or make sense) in this context.Chilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let rsa = chilkat::Rsa::new();
// First load a public key object with a public key.
// In this case, we'll load it from a file.
let pubkey = chilkat::PublicKey::new();
if pubkey.load_from_file("qa_data/pem/rsa_public.pem").is_err() {
println!("{}", pubkey.last_error_text());
return;
}
// RSA encryption is limited to small amounts of data. The limit
// is typically a few hundred bytes and is based on the key size and
// padding (OAEP vs. PKCS1_5). RSA encryption is typically used for
// encrypting hashes or symmetric (bulk encryption algorithm) secret keys.
let plain_text = "Time is an illusion. Lunchtime doubly so.".to_string();
// Import the public key to be used for encrypting.
let _ = rsa.use_public_key(&pubkey).is_ok();
// To get OAEP padding, set the PkcsPadding property equal to false
rsa.set_pkcs_padding(false);
rsa.set_oaep_hash("sha256");
// Indicate we'll want hex output
rsa.set_encoding_mode("hex");
// Encrypt..
let use_private_key = false;
let encrypted_str = rsa.encrypt_string_enc(&plain_text, use_private_key).unwrap_or_default();
println!("{}", encrypted_str);
// -------------------------------------------------
// Now decrypt with the matching private key.
let rsa2 = chilkat::Rsa::new();
let priv_key = chilkat::PrivateKey::new();
if priv_key.load_encrypted_pem("qa_data/pem/rsa_passwd.pem", "passwd").is_err() {
println!("{}", priv_key.last_error_text());
return;
}
let _ = rsa2.use_private_key(&priv_key).is_ok();
// Make sure we have the same settings used for encryption.
rsa2.set_pkcs_padding(false);
rsa2.set_encoding_mode("hex");
rsa2.set_oaep_hash("sha256");
let original_str = rsa2.decrypt_string_enc(&encrypted_str, true).unwrap_or_default();
println!("{}", original_str);