Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJwe
    String sJweCompact
    String sBase64Key
    Variant vSbContent
    Handle hoSbContent
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatJwe)) To hoJwe
    If (Not(IsComObjectCreated(hoJwe))) Begin
        Send CreateComObject of hoJwe
    End

    //  Load the JWE compact serialization to be decrypted.
    Move "eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2R0NNIn0.<...>.<iv>.<ciphertext>.<tag>" To sJweCompact
    Get ComLoadJwe Of hoJwe sJweCompact To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJwe To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Move "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=" To sBase64Key
    Get ComSetWrappingKey Of hoJwe 0 sBase64Key "base64" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJwe To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Decrypt recipient index 0, writing the plaintext into a StringBuilder.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbContent
    If (Not(IsComObjectCreated(hoSbContent))) Begin
        Send CreateComObject of hoSbContent
    End
    Get pvComObject of hoSbContent to vSbContent
    Get ComDecryptSb Of hoJwe 0 "utf-8" vSbContent To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJwe To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComGetAsString Of hoSbContent To sTemp1
    Showln sTemp1


End_Procedure