Sample code for 30+ languages & platforms
Unicode C

Load a Public Key from a String

Demonstrates the Chilkat PublicKey.LoadFromString method, which loads a public key from a string. The only argument is the string. Chilkat examines the content and recognizes PEM public keys, unencrypted PEM private keys (taking only the public portion), XML, JWK, and more.

Background: This is the forgiving text loader: rather than requiring you to know the exact format, it inspects whatever string you provide and figures it out. A useful convenience is that it accepts an unencrypted private key too and keeps only the public half — handy when you have a key pair but need just the public key. Use it whenever the textual format is uncertain or varies by source.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkPublicKeyW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPublicKeyW pubKey;
    const wchar_t *keyText;

    success = FALSE;

    //  Demonstrates the PublicKey.LoadFromString method, which loads a public key from a string.  The
    //  only argument is the string.  Chilkat examines the content and recognizes PEM public keys,
    //  unencrypted PEM private keys (using only the public portion), XML, JWK, and more.

    pubKey = CkPublicKeyW_Create();

    //  The PEM text of a public key.
    keyText = L"-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkq...IDAQAB\n-----END PUBLIC KEY-----";

    success = CkPublicKeyW_LoadFromString(pubKey,keyText);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPublicKeyW_lastErrorText(pubKey));
        CkPublicKeyW_Dispose(pubKey);
        return;
    }

    wprintf(L"Loaded a %s public key.\n",CkPublicKeyW_keyType(pubKey));


    CkPublicKeyW_Dispose(pubKey);

    }