Sample code for 30+ languages & platforms
Unicode C

RSA Import Public Key from Certificate PEM

See more RSA Examples

Uses a certificate's public key for RSA encryption. The public key from the certificate .pem file is used.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkCertW.h>
#include <C_CkPublicKeyW.h>
#include <C_CkRsaW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkCertW cert;
    HCkPublicKeyW pubKey;
    HCkRsaW rsa;
    const wchar_t *encryptedStr;

    success = FALSE;

    cert = CkCertW_Create();

    success = CkCertW_LoadFromFile(cert,L"qa_data/pem/mf_public_rsa.pem");
    if (success == FALSE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkCertW_Dispose(cert);
        return;
    }

    pubKey = CkPublicKeyW_Create();
    CkCertW_GetPublicKey(cert,pubKey);

    rsa = CkRsaW_Create();
    CkRsaW_UsePublicKey(rsa,pubKey);

    CkRsaW_putEncodingMode(rsa,L"base64");
    encryptedStr = CkRsaW_encryptStringENC(rsa,L"hello",FALSE);
    wprintf(L"encrypted string = %s\n",encryptedStr);


    CkCertW_Dispose(cert);
    CkPublicKeyW_Dispose(pubKey);
    CkRsaW_Dispose(rsa);

    }