Sample code for 30+ languages & platforms
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 C++ Downloads

C++
#include <CkRsa.h>
#include <CkPublicKey.h>
#include <CkPrivateKey.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.

    CkRsa rsa;

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

    success = pubkey.LoadFromString(publicKeyPem);
    if (success == false) {
        std::cout << pubkey.lastErrorText() << "\r\n";
        return;
    }

    success = rsa.UsePublicKey(pubkey);
    if (success == false) {
        std::cout << rsa.lastErrorText() << "\r\n";
        return;
    }

    // Demonstrate importing a PEM private key:
    const char *privateKeyPem = "PEM private-key data goes here";
    CkPrivateKey privkey;

    // If the private key PEM is protected with a password, then 
    // call LoadEncryptedPem.  Otherwise call LoadPem.
    success = privkey.LoadPem(privateKeyPem);
    if (success == false) {
        std::cout << privkey.lastErrorText() << "\r\n";
        return;
    }

    success = rsa.UsePrivateKey(privkey);
    if (success == false) {
        std::cout << rsa.lastErrorText() << "\r\n";
        return;
    }

    std::cout << "OK!" << "\r\n";
    }