Sample code for 30+ languages & platforms
Unicode C

RSA Signature using Private Key from .snk File

See more RSA Examples

A .snk file (Strong Name Key file) is a file format used in the .NET ecosystem to store an RSA public/private key pair. This key pair is typically used for strong-naming assemblies, which is a process of signing .NET assemblies to ensure their integrity and to uniquely identify them.

This example loads a private key from a .snk file and creates an RSA signature.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkRsaW rsa;
    const wchar_t *xmlStr;
    HCkPrivateKeyW privKey;
    const wchar_t *sigBase64;

    success = FALSE;

    rsa = CkRsaW_Create();

    //  Load the .snk and return the private key as XML.
    xmlStr = CkRsaW_snkToXml(rsa,L"./test.snk");
    if (CkRsaW_getLastMethodSuccess(rsa) == FALSE) {
        wprintf(L"%s\n",CkRsaW_lastErrorText(rsa));
        CkRsaW_Dispose(rsa);
        return;
    }

    privKey = CkPrivateKeyW_Create();
    success = CkPrivateKeyW_LoadXml(privKey,xmlStr);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(privKey));
        CkRsaW_Dispose(rsa);
        CkPrivateKeyW_Dispose(privKey);
        return;
    }

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

    //  Sign the SHA-256 hash of the utf-8 byte representation of the contents of sb
    //  Return the signature in base64 format.
    CkRsaW_putEncodingMode(rsa,L"base64");
    CkRsaW_putCharset(rsa,L"utf-8");
    sigBase64 = CkRsaW_signStringENC(rsa,L"This is the text to be hashed and signed.",L"sha256");
    if (CkRsaW_getLastMethodSuccess(rsa) == FALSE) {
        wprintf(L"%s\n",CkRsaW_lastErrorText(rsa));
        CkRsaW_Dispose(rsa);
        CkPrivateKeyW_Dispose(privKey);
        return;
    }

    wprintf(L"RSA signature as base64: %s\n",sigBase64);


    CkRsaW_Dispose(rsa);
    CkPrivateKeyW_Dispose(privKey);

    }