Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

RSA Encrypt and OpenSSL Decrypt

See more OpenSSL Examples

Demonstrates how to use Chilkat to RSA encrypt, and then use OpenSSL to decrypt.

Chilkat Rust Downloads

Rust

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

let rsa = chilkat::Rsa::new();

let priv_key = chilkat::PrivateKey::new();
let _ = rsa.gen_key(2048, &priv_key).is_ok();
let _ = priv_key.save_pkcs8_pem_file("qa_output/privKey.pem").is_ok();

let pub_key = chilkat::PublicKey::new();
let _ = priv_key.to_public_key(&pub_key);

rsa.set_encoding_mode("base64");
let plain_text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890".to_string();
let b_use_private_key = false;
let _ = rsa.use_public_key(&pub_key);
let encrypted_str = rsa.encrypt_string_enc(&plain_text, b_use_private_key).unwrap_or_default();

let bd = chilkat::BinData::new();
let _ = bd.append_encoded(&encrypted_str, "base64");
let _ = bd.write_file("qa_output/enc.dat").is_ok();

// The OpenSSL command to decrypt is:
// openssl pkeyutl -in enc.dat -inkey privKey.pem -keyform PEM -pkeyopt rsa_padding_mode:pkcs1 -decrypt

println!("OK");