Sample code for 30+ languages & platforms
Unicode C++

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 Unicode C++ Downloads

Unicode C++
#include <CkPrivateKeyW.h>
#include <CkXmlW.h>
#include <CkBinDataW.h>
#include <CkCertW.h>
#include <CkPublicKeyW.h>

void ChilkatSample(void)
    {
    bool success = false;

    CkPrivateKeyW privKey;

    const wchar_t *password = L"12345678a";
    success = privKey.LoadPkcs8EncryptedFile(L"qa_data/certs/test_12345678a.key",password);
    if (success == false) {
        wprintf(L"%s\n",privKey.lastErrorText());
        return;
    }

    CkXmlW xml;
    xml.LoadXml(privKey.getXml());

    // The XML contains the parts of the key in base64.
    wprintf(L"Private Key XML:\n");
    wprintf(L"%s\n",xml.getXml());

    // We can get the base64 modulus like this:
    const wchar_t *modulus = xml.getChildContent(L"Modulus");
    wprintf(L"base64 modulus = %s\n",modulus);

    // To convert to hex:
    CkBinDataW binDat;
    binDat.AppendEncoded(modulus,L"base64");
    const wchar_t *hexModulus = binDat.getEncoded(L"hex");
    wprintf(L"hex modulus = %s\n",hexModulus);

    // Now get the modulus from the cert:
    CkCertW cert;

    success = cert.LoadFromFile(L"qa_data/certs/test_12345678a.cer");
    if (success == false) {
        wprintf(L"%s\n",cert.lastErrorText());
        return;
    }

    // The cert contains the public key, which is composed of the
    // modulus + exponent (for RSA keys).
    CkPublicKeyW pubKey;
    cert.GetPublicKey(pubKey);

    xml.LoadXml(pubKey.getXml());
    wprintf(L"Public Key XML:\n");
    wprintf(L"%s\n",xml.getXml());

    // Proceed in the same way as before....
    modulus = xml.getChildContent(L"Modulus");
    wprintf(L"base64 modulus = %s\n",modulus);

    // To convert to hex:
    binDat.Clear();
    binDat.AppendEncoded(modulus,L"base64");
    hexModulus = binDat.getEncoded(L"hex");
    wprintf(L"hex modulus = %s\n",hexModulus);
    }