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

Load PEM Public/Private Key into RSA Object

See more RSA Examples

Demonstrates how to load a PEM key into the Chilkat RSA object.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkRsaW.h>
#include <CkPublicKeyW.h>
#include <CkPrivateKeyW.h>

void ChilkatSample(void)
    {
    bool success = false;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkRsaW rsa;

    // First demonstrate importing a PEM public key:
    const wchar_t *publicKeyPem = L"PEM public-key data goes here";
    CkPublicKeyW pubkey;

    success = pubkey.LoadFromString(publicKeyPem);
    if (success == false) {
        wprintf(L"%s\n",pubkey.lastErrorText());
        return;
    }

    success = rsa.UsePublicKey(pubkey);
    if (success == false) {
        wprintf(L"%s\n",rsa.lastErrorText());
        return;
    }

    // Demonstrate importing a PEM private key:
    const wchar_t *privateKeyPem = L"PEM private-key data goes here";
    CkPrivateKeyW privkey;

    // If the private key PEM is protected with a password, then 
    // call LoadEncryptedPem.  Otherwise call LoadPem.
    success = privkey.LoadPem(privateKeyPem);
    if (success == false) {
        wprintf(L"%s\n",privkey.lastErrorText());
        return;
    }

    success = rsa.UsePrivateKey(privkey);
    if (success == false) {
        wprintf(L"%s\n",rsa.lastErrorText());
        return;
    }

    wprintf(L"OK!\n");
    }