Sample code for 30+ languages & platforms
Unicode C

Decrypt 256-bit AES GCM Produced by Something Unknown

See more Encryption Examples

Demonstrates how to decrypt something produced elsewhere (unknown) with 256-bit AES GCM.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    const wchar_t *keyBase64;
    const wchar_t *ivBase64Url;
    const wchar_t *cipherBase64Url;
    HCkCrypt2W crypt;
    HCkBinDataW bdEncrypted;
    HCkBinDataW bdAuthTag;
    int numBytes;
    const wchar_t *authTagHex;
    const wchar_t *originalText;

    success = FALSE;

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

    // We have the following to decrypt:

    // Key (Base64): 
    keyBase64 = L"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    // IV (Base64Url):
    ivBase64Url = L"xrvaINMLqotAbWRK";

    // ciphertext (base64url):
    cipherBase64Url = L"RtGNAS-zQOxSB8W0HfqJjCoyt9KgImW_l-HjVC40hOOl-RNfRF3hzDIT1kvFVF8i_KX9XmqAftb6lyq-jLCEc_MSgqt3q1ixv3Ez4SbS3G5e3qGzLwxIMi2sCt00aDNwK2ipsJ4aw8s7ePPnl4oY-y1st9rwCWR0rrgEZwS9jmS4uJWGPn9K3jbKRnMslznDbtFLNJctMVXBTP-cv47JelxLCBOQSlK29rMuEFrhHR_VQrPq6gtZaBVSXZSYT0XOklp7nu9mVhrMCRtBCC5oiu5MPE5JYx4ANo3hUY7_NyQl2bpn9GfRXrdvqRGE-gy2upj-cDkm0t_tV8xmYge9DBQTH3B_4BGl2qTk_o-m7pEmKkS8XSdQhGcuFlykqrkE8SzB5I8esfzWOM0pwxbz0H_VaylKYHY=";

    crypt = CkCrypt2W_Create();

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

    success = CkCrypt2W_SetEncodedAad(crypt,L"random",L"ascii");
    CkCrypt2W_SetEncodedKey(crypt,keyBase64,L"base64");
    CkCrypt2W_SetEncodedIV(crypt,ivBase64Url,L"base64url");

    // The cipher text contains the 16-byte auth tag at the end.
    // get it separately..
    bdEncrypted = CkBinDataW_Create();
    bdAuthTag = CkBinDataW_Create();
    success = CkBinDataW_AppendEncoded(bdEncrypted,cipherBase64Url,L"base64url");

    numBytes = CkBinDataW_getNumBytes(bdEncrypted);
    authTagHex = CkBinDataW_getEncodedChunk(bdEncrypted,numBytes - 16,16,L"hex");

    wprintf(L"Auth tag in hex: %s\n",authTagHex);

    success = CkBinDataW_AppendEncoded(bdAuthTag,authTagHex,L"hex");
    CkBinDataW_RemoveChunk(bdEncrypted,numBytes - 16,16);

    // Use this special value to tell Chilkat to ignore the auth tag.
    success = CkCrypt2W_SetEncodedAuthTag(crypt,L"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",L"hex");

    // Decrypt
    CkCrypt2W_putEncodingMode(crypt,L"base64");
    originalText = CkCrypt2W_decryptStringENC(crypt,CkBinDataW_getEncoded(bdEncrypted,L"base64"));
    if (CkCrypt2W_getLastMethodSuccess(crypt) == FALSE) {
        wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt));
    }
    else {
        wprintf(L"%s\n",originalText);
        wprintf(L"Success.\n");
    }

    // Decrypted text


    CkCrypt2W_Dispose(crypt);
    CkBinDataW_Dispose(bdEncrypted);
    CkBinDataW_Dispose(bdAuthTag);

    }