Sample code for 30+ languages & platforms
Unicode C

Decrypt a JWE into BinData

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.DecryptBd, which decrypts a recipient of a loaded JWE and writes the plaintext bytes into a BinData.

Background. This is used when the decrypted content is binary. The recipient key must be provided before decrypting.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkJweW jwe;
    const wchar_t *jweCompact;
    const wchar_t *base64Key;
    HCkBinDataW bd;

    success = FALSE;

    jwe = CkJweW_Create();

    //  Load the JWE compact serialization to be decrypted.
    jweCompact = L"eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2R0NNIn0.<...>.<iv>.<ciphertext>.<tag>";
    success = CkJweW_LoadJwe(jwe,jweCompact);
    if (success == FALSE) {
        wprintf(L"%s\n",CkJweW_lastErrorText(jwe));
        CkJweW_Dispose(jwe);
        return;
    }

    //  The 256-bit key-wrapping key (base64) for recipient index 0.  In production, obtain the key
    //  from a secure source rather than hard-coding it.
    base64Key = L"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=";
    success = CkJweW_SetWrappingKey(jwe,0,base64Key,L"base64");
    if (success == FALSE) {
        wprintf(L"%s\n",CkJweW_lastErrorText(jwe));
        CkJweW_Dispose(jwe);
        return;
    }

    //  Decrypt recipient index 0, writing the plaintext bytes into a BinData.
    bd = CkBinDataW_Create();
    success = CkJweW_DecryptBd(jwe,0,bd);
    if (success == FALSE) {
        wprintf(L"%s\n",CkJweW_lastErrorText(jwe));
        CkJweW_Dispose(jwe);
        CkBinDataW_Dispose(bd);
        return;
    }

    wprintf(L"Decrypted %d bytes.\n",CkBinDataW_getNumBytes(bd));


    CkJweW_Dispose(jwe);
    CkBinDataW_Dispose(bd);

    }