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

Generate RSA Key and return Base64 PKCS8 Private Key

See more RSA Examples

Demonstrates how to generate a new 2048-bit RSA private key and returns the Base64 encoded PKCS8 representation of the private key.

Chilkat Rust Downloads

Rust

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

// Generate a 2048-bit key.  Chilkat RSA supports
// key sizes ranging from 512 bits to 8192 bits.
let priv_key = chilkat::PrivateKey::new();
if rsa.gen_key(2048, &priv_key).is_err() {
    println!("{}", rsa.last_error_text());
    return;
}

// Get the private key in PKCS8 Base64 format
let priv_key_pkcs8_base64 = priv_key.get_pkcs8_enc("base64").unwrap_or_default();

// The key in base64 format will start similar to this:
// MIIEvAIBADANBgkqhkiG9w0BA...
println!("{}", priv_key_pkcs8_base64);