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

Convert RSA Private Key to Public Key

See more RSA Examples

Demonstrates how to get a public RSA key from a private RSA key.

Chilkat Rust Downloads

Rust

let priv_key = chilkat::PrivateKey::new();

// Step 1: Load the private key from a source.
// (Chilkat can load private keys from all types of formats, and from in-memory bytes or encoded strings.
// see the online reference documentation for more options.)
if priv_key.load_pem_file("qa_data/pem/VP_Private.pem").is_err() {
    println!("{}", priv_key.last_error_text());
    return;
}

// Step 2: Get the public key object from the private key object.
let pub_key = chilkat::PublicKey::new();
let _ = priv_key.to_public_key(&pub_key);

// Step 3: Save the public key in a desired format. 
// (Chilkat can load or save public and private keys in many different formats.  See
// the online reference documentation for more options.)

// Saves to a PKCS8 PEM file.
let b_prefer_pkcs1 = false;
let _ = pub_key.save_pem_file(b_prefer_pkcs1, "qa_data/pem/VP_Public.pem").is_ok();
if !pub_key.last_method_success() {
    println!("{}", pub_key.last_error_text());
    return;
}

println!("Extracted and saved public key from private key.");