Sample code for 30+ languages & platforms
Unicode C

Decrypt a JWE into a StringBuilder

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.DecryptSb, which decrypts a recipient of a loaded JWE and writes the plaintext into a StringBuilder.

Background. The recipient index selects which recipient's key material to use. The key for that recipient must be provided before decrypting.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkJweW jwe;
    const wchar_t *jweCompact;
    const wchar_t *base64Key;
    HCkStringBuilderW sbContent;

    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 into a StringBuilder.
    sbContent = CkStringBuilderW_Create();
    success = CkJweW_DecryptSb(jwe,0,L"utf-8",sbContent);
    if (success == FALSE) {
        wprintf(L"%s\n",CkJweW_lastErrorText(jwe));
        CkJweW_Dispose(jwe);
        CkStringBuilderW_Dispose(sbContent);
        return;
    }

    wprintf(L"%s\n",CkStringBuilderW_getAsString(sbContent));


    CkJweW_Dispose(jwe);
    CkStringBuilderW_Dispose(sbContent);

    }