Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

loJwe = createobject("CkJwe")

//  Load the JWE compact serialization to be decrypted.
lcJweCompact = "eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2R0NNIn0.<...>.<iv>.<ciphertext>.<tag>"
llSuccess = loJwe.LoadJwe(lcJweCompact)
if (llSuccess = .F.) then
    ? loJwe.LastErrorText
    release loJwe
    return
endif

//  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.
loProtHeader = createobject("CkJsonObject")
llSuccess = loJwe.GetProtectedHeader(loProtHeader)
if (llSuccess = .F.) then
    ? loJwe.LastErrorText
    release loJwe
    release loProtHeader
    return
endif

//  Read the alg (key-management) and enc (content-encryption) header values into StringBuilder
//  objects so they can be compared.
loSbAlg = createobject("CkStringBuilder")
llSuccess = loProtHeader.StringOfSb("alg",loSbAlg)
if (llSuccess = .F.) then
    ? loProtHeader.LastErrorText
    release loJwe
    release loProtHeader
    release loSbAlg
    return
endif

loSbEnc = createobject("CkStringBuilder")
llSuccess = loProtHeader.StringOfSb("enc",loSbEnc)
if (llSuccess = .F.) then
    ? loProtHeader.LastErrorText
    release loJwe
    release loProtHeader
    release loSbAlg
    release loSbEnc
    return
endif

? "JWE algorithms: alg=" + loSbAlg.GetAsString() + " enc=" + loSbEnc.GetAsString()

//  Enforce an allowlist of acceptable algorithms.  Reject anything not permitted by policy.  String
//  values are compared using StringBuilder.ContentsEqual.
llBCaseSensitive = .T.
if (loSbAlg.ContentsEqual("A256KW",llBCaseSensitive) <> .T.) then
    ? "Rejecting JWE: key-management algorithm is not permitted by policy."
    release loJwe
    release loProtHeader
    release loSbAlg
    release loSbEnc
    return
endif

if (loSbEnc.ContentsEqual("A256GCM",llBCaseSensitive) <> .T.) then
    ? "Rejecting JWE: content-encryption algorithm is not permitted by policy."
    release loJwe
    release loProtHeader
    release loSbAlg
    release loSbEnc
    return
endif

//  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.
lnRecipientIndex = loJwe.FindRecipient("kid","recipient-key-1",llBCaseSensitive)
if (lnRecipientIndex < 0) then
    lnRecipientIndex = 0
endif

//  Provide the key for the selected recipient.  In production, obtain the key from a secure source
//  rather than hard-coding it.
lcBase64Key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU="
llSuccess = loJwe.SetWrappingKey(lnRecipientIndex,lcBase64Key,"base64")
if (llSuccess = .F.) then
    ? loJwe.LastErrorText
    release loJwe
    release loProtHeader
    release loSbAlg
    release loSbEnc
    return
endif

//  Decrypt the selected recipient and return the plaintext, decoded using the given charset.
lcContent = loJwe.Decrypt(lnRecipientIndex,"utf-8")
if (loJwe.LastMethodSuccess = .F.) then
    ? loJwe.LastErrorText
    release loJwe
    release loProtHeader
    release loSbAlg
    release loSbEnc
    return
endif

? lcContent


release loJwe
release loProtHeader
release loSbAlg
release loSbEnc