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

Load PEM Public/Private Key into RSA Object

See more RSA Examples

Demonstrates how to load a PEM key into the Chilkat RSA object.

Chilkat Rust Downloads

Rust

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

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

// First demonstrate importing a PEM public key:
let public_key_pem = "PEM public-key data goes here".to_string();
let pubkey = chilkat::PublicKey::new();

if pubkey.load_from_string(&public_key_pem).is_err() {
    println!("{}", pubkey.last_error_text());
    return;
}

if rsa.use_public_key(&pubkey).is_err() {
    println!("{}", rsa.last_error_text());
    return;
}

// Demonstrate importing a PEM private key:
let private_key_pem = "PEM private-key data goes here".to_string();
let privkey = chilkat::PrivateKey::new();

// If the private key PEM is protected with a password, then 
// call LoadEncryptedPem.  Otherwise call LoadPem.
if privkey.load_pem(&private_key_pem).is_err() {
    println!("{}", privkey.last_error_text());
    return;
}

if rsa.use_private_key(&privkey).is_err() {
    println!("{}", rsa.last_error_text());
    return;
}

println!("OK!");