Sample code for 30+ languages & platforms
Unicode C++

Decrypt a JWE to a String

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.Decrypt, which decrypts a recipient of a loaded JWE and returns the plaintext, decoded using the given charset. Before decrypting, the example inspects the JWE protected header and, under application policy, accepts only permitted algorithms and selects an acceptable recipient.

Background. A received JWE should never be trusted to specify safe algorithms. The example reads the alg and enc values from the protected header (via GetProtectedHeader) and enforces an allowlist, rejecting anything not permitted. It then selects the recipient the application holds a key for — using FindRecipient to locate a recipient by a per-recipient header value such as kid, falling back to index 0 for a single-recipient compact JWE — and only then supplies the key and decrypts.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkJweW.h>
#include <CkJsonObjectW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

    CkJweW jwe;

    //  Load the JWE compact serialization to be decrypted.
    const wchar_t *jweCompact = L"eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2R0NNIn0.<...>.<iv>.<ciphertext>.<tag>";
    success = jwe.LoadJwe(jweCompact);
    if (success == false) {
        wprintf(L"%s\n",jwe.lastErrorText());
        return;
    }

    //  Application policy: before decrypting, inspect the JWE header and accept only algorithms the
    //  application trusts.  Never assume the algorithms in a received JWE are safe -- a sender could
    //  specify a weak or unexpected algorithm.
    CkJsonObjectW protHeader;
    success = jwe.GetProtectedHeader(protHeader);
    if (success == false) {
        wprintf(L"%s\n",jwe.lastErrorText());
        return;
    }

    //  Read the alg (key-management) and enc (content-encryption) header values into StringBuilder
    //  objects so they can be compared.
    CkStringBuilderW sbAlg;
    success = protHeader.StringOfSb(L"alg",sbAlg);
    if (success == false) {
        wprintf(L"%s\n",protHeader.lastErrorText());
        return;
    }

    CkStringBuilderW sbEnc;
    success = protHeader.StringOfSb(L"enc",sbEnc);
    if (success == false) {
        wprintf(L"%s\n",protHeader.lastErrorText());
        return;
    }

    wprintf(L"JWE algorithms: alg=%s enc=%s\n",sbAlg.getAsString(),sbEnc.getAsString());

    //  Enforce an allowlist of acceptable algorithms.  Reject anything not permitted by policy.  String
    //  values are compared using StringBuilder.ContentsEqual.
    bool bCaseSensitive = true;
    if (sbAlg.ContentsEqual(L"A256KW",bCaseSensitive) != true) {
        wprintf(L"Rejecting JWE: key-management algorithm is not permitted by policy.\n");
        return;
    }

    if (sbEnc.ContentsEqual(L"A256GCM",bCaseSensitive) != true) {
        wprintf(L"Rejecting JWE: content-encryption algorithm is not permitted by policy.\n");
        return;
    }

    //  Select an acceptable recipient.  For a multi-recipient JWE, FindRecipient locates the recipient
    //  this application holds a key for by a per-recipient header value such as "kid".  A compact JWE
    //  has a single recipient at index 0, so FindRecipient returns -1; fall back to index 0 in that case.
    int recipientIndex = jwe.FindRecipient(L"kid",L"recipient-key-1",bCaseSensitive);
    if (recipientIndex < 0) {
        recipientIndex = 0;
    }

    //  Provide the key for the selected recipient.  In production, obtain the key from a secure source
    //  rather than hard-coding it.
    const wchar_t *base64Key = L"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=";
    success = jwe.SetWrappingKey(recipientIndex,base64Key,L"base64");
    if (success == false) {
        wprintf(L"%s\n",jwe.lastErrorText());
        return;
    }

    //  Decrypt the selected recipient and return the plaintext, decoded using the given charset.
    const wchar_t *content = jwe.decrypt(recipientIndex,L"utf-8");
    if (jwe.get_LastMethodSuccess() == false) {
        wprintf(L"%s\n",jwe.lastErrorText());
        return;
    }

    wprintf(L"%s\n",content);
    }