Sample code for 30+ languages & platforms
Unicode C

Example: Crypt2.RandomizeIV method

Demonstrates using a random initialization vector for AES GCM encryption.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkCrypt2W.h>
#include <C_CkBinDataW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkCrypt2W crypt;
    const wchar_t *K;
    const wchar_t *AAD;
    const wchar_t *PT;
    const wchar_t *IV;
    const wchar_t *cipherText;
    const wchar_t *authTag;
    HCkBinDataW bdEncrypted;
    const wchar_t *concatenatedGcmOutput;
    HCkCrypt2W decrypt;
    HCkBinDataW bdFromEncryptor;
    int sz;
    const wchar_t *extractedIV;
    const wchar_t *extractedCipherText;
    const wchar_t *expectedAuthTag;
    const wchar_t *decryptedText;

    success = FALSE;

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

    crypt = CkCrypt2W_Create();

    CkCrypt2W_putCryptAlgorithm(crypt,L"aes");
    CkCrypt2W_putCipherMode(crypt,L"gcm");
    CkCrypt2W_putKeyLength(crypt,256);

    K = L"000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F";
    AAD = L"feedfacedeadbeeffeedfacedeadbeefabaddad2";
    PT = L"This is the text to be AES-GCM encrypted.";

    //  Generate a random IV.
    CkCrypt2W_RandomizeIV(crypt);
    IV = CkCrypt2W_getEncodedIV(crypt,L"hex");

    CkCrypt2W_SetEncodedKey(crypt,K,L"hex");

    success = CkCrypt2W_SetEncodedAad(crypt,AAD,L"hex");

    //  Return the encrypted bytes as base64
    CkCrypt2W_putEncodingMode(crypt,L"base64");
    CkCrypt2W_putCharset(crypt,L"utf-8");
    cipherText = CkCrypt2W_encryptStringENC(crypt,PT);
    if (CkCrypt2W_getLastMethodSuccess(crypt) != TRUE) {
        wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt));
        CkCrypt2W_Dispose(crypt);
        return;
    }

    //  Get the GCM authenticated tag computed when encrypting.
    authTag = CkCrypt2W_getEncodedAuthTag(crypt,L"base64");

    wprintf(L"Cipher Text: %s\n",cipherText);
    wprintf(L"Auth Tag: %s\n",authTag);

    //  Let's send the IV, CipherText, and AuthTag to the decrypting party.
    //  We'll send them concatenated like this: [IV || Ciphertext || AuthTag]
    //  In base64 format.
    bdEncrypted = CkBinDataW_Create();
    CkBinDataW_AppendEncoded(bdEncrypted,IV,L"hex");
    CkBinDataW_AppendEncoded(bdEncrypted,cipherText,L"base64");
    CkBinDataW_AppendEncoded(bdEncrypted,authTag,L"base64");

    concatenatedGcmOutput = CkBinDataW_getEncoded(bdEncrypted,L"base64");
    wprintf(L"Concatenated GCM Output: %s\n",concatenatedGcmOutput);

    //  Sample output so far:

    //  -------------------------------------------------------------------------------------
    //  Now let's GCM decrypt...
    //  -------------------------------------------------------------------------------------

    decrypt = CkCrypt2W_Create();

    //  The values shared and agreed upon by both sides beforehand are: algorithm, cipher mode, secret key, and AAD.
    //  Sometimes the IV can be a value already known and agreed upon, but in this case the encryptor sends the IV to the decryptor.
    CkCrypt2W_putCryptAlgorithm(decrypt,L"aes");
    CkCrypt2W_putCipherMode(decrypt,L"gcm");
    CkCrypt2W_putKeyLength(decrypt,256);
    CkCrypt2W_SetEncodedKey(decrypt,K,L"hex");
    CkCrypt2W_SetEncodedAad(decrypt,AAD,L"hex");

    bdFromEncryptor = CkBinDataW_Create();
    CkBinDataW_AppendEncoded(bdFromEncryptor,concatenatedGcmOutput,L"base64");

    sz = CkBinDataW_getNumBytes(bdFromEncryptor);

    //  Extract the parts.
    extractedIV = CkBinDataW_getEncodedChunk(bdFromEncryptor,0,16,L"hex");
    extractedCipherText = CkBinDataW_getEncodedChunk(bdFromEncryptor,16,sz - 32,L"base64");
    expectedAuthTag = CkBinDataW_getEncodedChunk(bdFromEncryptor,sz - 16,16,L"base64");

    //  Before GCM decrypting, we must set the authenticated tag to the value that is expected.
    //  The decryption will fail if the resulting authenticated tag is not equal to the expected result.
    success = CkCrypt2W_SetEncodedAuthTag(decrypt,expectedAuthTag,L"base64");

    //  Also set the IV.
    CkCrypt2W_SetEncodedIV(decrypt,extractedIV,L"hex");

    //  Decrypt..
    CkCrypt2W_putEncodingMode(decrypt,L"base64");
    CkCrypt2W_putCharset(decrypt,L"utf-8");
    decryptedText = CkCrypt2W_decryptStringENC(decrypt,extractedCipherText);
    if (CkCrypt2W_getLastMethodSuccess(decrypt) != TRUE) {
        //  Failed.  The resultant authenticated tag did not equal the expected authentication tag.
        wprintf(L"%s\n",CkCrypt2W_lastErrorText(decrypt));
        CkCrypt2W_Dispose(crypt);
        CkBinDataW_Dispose(bdEncrypted);
        CkCrypt2W_Dispose(decrypt);
        CkBinDataW_Dispose(bdFromEncryptor);
        return;
    }

    wprintf(L"Decrypted: %s\n",decryptedText);


    CkCrypt2W_Dispose(crypt);
    CkBinDataW_Dispose(bdEncrypted);
    CkCrypt2W_Dispose(decrypt);
    CkBinDataW_Dispose(bdFromEncryptor);

    }