Sample code for 30+ languages & platforms
Unicode C

RSA Sign with PKCS8 Encrypted Key

See more RSA Examples

Demonstrates how to load a private key from an encrypted PKCS8 file and create an RSA digital signature (and then verify it).

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkPrivateKeyW privKey;
    HCkRsaW rsa;
    const wchar_t *strData;
    const wchar_t *hexSig;
    HCkCertW cert;
    HCkPublicKeyW pubKey;
    HCkRsaW rsa2;

    success = FALSE;

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

    privKey = CkPrivateKeyW_Create();

    // Load the private key from an RSA PEM file:
    success = CkPrivateKeyW_LoadAnyFormatFile(privKey,L"raul_privateKey.key",L"a0123456789");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(privKey));
        CkPrivateKeyW_Dispose(privKey);
        return;
    }

    rsa = CkRsaW_Create();

    // Import the private key into the RSA component:
    success = CkRsaW_UsePrivateKey(rsa,privKey);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRsaW_lastErrorText(rsa));
        CkPrivateKeyW_Dispose(privKey);
        CkRsaW_Dispose(rsa);
        return;
    }

    // This example will sign a string, and receive the signature
    // in a hex-encoded string.  Therefore, set the encoding mode
    // to "hex":
    CkRsaW_putEncodingMode(rsa,L"hex");

    strData = L"This is the string to be signed.";

    // Sign the string using the sha256 hash algorithm.
    // Other valid choices are sha1, sha384, sha512 and others.
    hexSig = CkRsaW_signStringENC(rsa,strData,L"sha256");
    if (CkRsaW_getLastMethodSuccess(rsa) == FALSE) {
        wprintf(L"%s\n",CkRsaW_lastErrorText(rsa));
        CkPrivateKeyW_Dispose(privKey);
        CkRsaW_Dispose(rsa);
        return;
    }

    wprintf(L"%s\n",hexSig);

    // Now verify with the public key.
    // This example shows how to use the public key from 
    // a digital certificate (.cer file)
    cert = CkCertW_Create();
    success = CkCertW_LoadFromFile(cert,L"raul_publicKey.cer");
    if (success == FALSE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkPrivateKeyW_Dispose(privKey);
        CkRsaW_Dispose(rsa);
        CkCertW_Dispose(cert);
        return;
    }

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

    rsa2 = CkRsaW_Create();
    success = CkRsaW_UsePublicKey(rsa2,pubKey);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRsaW_lastErrorText(rsa2));
        CkPrivateKeyW_Dispose(privKey);
        CkRsaW_Dispose(rsa);
        CkCertW_Dispose(cert);
        CkPublicKeyW_Dispose(pubKey);
        CkRsaW_Dispose(rsa2);
        return;
    }

    // Verify the signature against the original data:
    CkRsaW_putEncodingMode(rsa2,L"hex");
    success = CkRsaW_VerifyStringENC(rsa2,strData,L"sha256",hexSig);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRsaW_lastErrorText(rsa2));
        CkPrivateKeyW_Dispose(privKey);
        CkRsaW_Dispose(rsa);
        CkCertW_Dispose(cert);
        CkPublicKeyW_Dispose(pubKey);
        CkRsaW_Dispose(rsa2);
        return;
    }

    wprintf(L"Signature verified!\n");

    // Verify with incorrect data:
    success = CkRsaW_VerifyStringENC(rsa2,L"something else",L"sha256",hexSig);
    if (success != TRUE) {
        wprintf(L"Signature not verified! (which was expected in this case)\n");
    }
    else {
        wprintf(L"Hmmm... that's not right...\n");
    }



    CkPrivateKeyW_Dispose(privKey);
    CkRsaW_Dispose(rsa);
    CkCertW_Dispose(cert);
    CkPublicKeyW_Dispose(pubKey);
    CkRsaW_Dispose(rsa2);

    }