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 <C_CkRsaW.h>
#include <C_CkPublicKeyW.h>
#include <C_CkPrivateKeyW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRsaW rsa;
    const wchar_t *publicKeyPem;
    HCkPublicKeyW pubkey;
    const wchar_t *privateKeyPem;
    HCkPrivateKeyW privkey;

    success = FALSE;

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

    rsa = CkRsaW_Create();

    // First demonstrate importing a PEM public key:
    publicKeyPem = L"PEM public-key data goes here";
    pubkey = CkPublicKeyW_Create();

    success = CkPublicKeyW_LoadFromString(pubkey,publicKeyPem);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPublicKeyW_lastErrorText(pubkey));
        CkRsaW_Dispose(rsa);
        CkPublicKeyW_Dispose(pubkey);
        return;
    }

    success = CkRsaW_UsePublicKey(rsa,pubkey);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRsaW_lastErrorText(rsa));
        CkRsaW_Dispose(rsa);
        CkPublicKeyW_Dispose(pubkey);
        return;
    }

    // Demonstrate importing a PEM private key:
    privateKeyPem = L"PEM private-key data goes here";
    privkey = CkPrivateKeyW_Create();

    // If the private key PEM is protected with a password, then 
    // call LoadEncryptedPem.  Otherwise call LoadPem.
    success = CkPrivateKeyW_LoadPem(privkey,privateKeyPem);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(privkey));
        CkRsaW_Dispose(rsa);
        CkPublicKeyW_Dispose(pubkey);
        CkPrivateKeyW_Dispose(privkey);
        return;
    }

    success = CkRsaW_UsePrivateKey(rsa,privkey);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRsaW_lastErrorText(rsa));
        CkRsaW_Dispose(rsa);
        CkPublicKeyW_Dispose(pubkey);
        CkPrivateKeyW_Dispose(privkey);
        return;
    }

    wprintf(L"OK!\n");


    CkRsaW_Dispose(rsa);
    CkPublicKeyW_Dispose(pubkey);
    CkPrivateKeyW_Dispose(privkey);

    }