Sample code for 30+ languages & platforms
Rust

Get RSA Key Modulus from .cer or .key

See more Certificates Examples

Demonstrates how to get the RSA key modulus from either the certificate (.cer) or RSA key (.key). OpenSSL commands to do the same would be:
openssl x509 -inform DER -in "test.cer"  -modulus -noout 
or
openssl pkcs8 -inform DER -in​ "test.key"​ -outform PEM -passin pass:"12345​678a​"
   | openssl rsa -inform PEM -modulus -noout 

Chilkat Rust Downloads

Rust

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

let password = "12345678a".to_string();
if priv_key.load_pkcs8_encrypted_file("qa_data/certs/test_12345678a.key", &password).is_err() {
    println!("{}", priv_key.last_error_text());
    return;
}

let xml = chilkat::Xml::new();
let _ = xml.load_xml(&priv_key.get_xml().unwrap_or_default());

// The XML contains the parts of the key in base64.
println!("Private Key XML:");
println!("{}", xml.get_xml().unwrap_or_default());

// We can get the base64 modulus like this:
let mut modulus = xml.get_child_content("Modulus").unwrap_or_default();
println!("base64 modulus = {}", modulus);

// To convert to hex:
let bin_dat = chilkat::BinData::new();
let _ = bin_dat.append_encoded(&modulus, "base64");
let mut hex_modulus = bin_dat.get_encoded("hex").unwrap_or_default();
println!("hex modulus = {}", hex_modulus);

// Now get the modulus from the cert:
let cert = chilkat::Cert::new();

if cert.load_from_file("qa_data/certs/test_12345678a.cer").is_err() {
    println!("{}", cert.last_error_text());
    return;
}

// The cert contains the public key, which is composed of the
// modulus + exponent (for RSA keys).
let pub_key = chilkat::PublicKey::new();
let _ = cert.get_public_key(&pub_key);

let _ = xml.load_xml(&pub_key.get_xml().unwrap_or_default());
println!("Public Key XML:");
println!("{}", xml.get_xml().unwrap_or_default());

// Proceed in the same way as before....
modulus = xml.get_child_content("Modulus").unwrap_or_default();
println!("base64 modulus = {}", modulus);

// To convert to hex:
let _ = bin_dat.clear();
let _ = bin_dat.append_encoded(&modulus, "base64");
hex_modulus = bin_dat.get_encoded("hex").unwrap_or_default();
println!("hex modulus = {}", hex_modulus);