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

Export Digital Certificate's Public Key

See more Certificates Examples

The ExportPublicKey method can be called to get a certificate's public key. It can then be saved to any of a number of formats: (1) OpenSSL DER, (2) OpenSSL PEM, (3) RSA DER, (4) XML.

Chilkat Rust Downloads

Rust

let cert = chilkat::Cert::new();

// LoadFromFile will load virtually any certificate format file.
// It will auto-recognize the format and load appropiately.
if cert.load_from_file("/Users/chilkat/testData/cer/chilkat.cer").is_err() {
    println!("{}", cert.last_error_text());
    return;
}

// Get the public key:
let pubkey = chilkat::PublicKey::new();
let _ = cert.get_public_key(&pubkey);

// Save to various formats:
if pubkey.save_der_file(false, "/Users/chilkat/testData/pubkeys/chilkat_pkcs8.der").is_err() {
    println!("{}", pubkey.last_error_text());
    return;
}

if pubkey.save_pem_file(false, "/Users/chilkat/testData/pubkeys/chilkat.pem").is_err() {
    println!("{}", pubkey.last_error_text());
    return;
}

if pubkey.save_der_file(true, "/Users/chilkat/testData/pubkeys/chilkat_pkcs1.der").is_err() {
    println!("{}", pubkey.last_error_text());
    return;
}

if pubkey.save_xml_file("/Users/chilkat/testData/pubkeys/chilkat.xml").is_err() {
    println!("{}", pubkey.last_error_text());
    return;
}

println!("Public key exported to all file formats.");