Sample code for 30+ languages & platforms
DataFlex

Load a JWE from a StringBuilder

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.LoadJweSb, which loads a JWE from a StringBuilder (here read from a file) so it can be decrypted.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. JWE (JSON Web Encryption) protects content with a content-encryption algorithm (the header enc value) while the content-encryption key is managed by the key-management algorithm (the header alg value). This example uses AES key wrapping with a symmetric wrapping key.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJwe
    Variant vSbJwe
    Handle hoSbJwe
    Boolean iBLoaded
    String sBase64Key
    String sContent
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    //  The file path is relative to the application's current working directory.  An absolute path may
    //  also be used.  Supply the path appropriate to your own environment.

    //  Load the JWE text from a file into a StringBuilder.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJwe
    If (Not(IsComObjectCreated(hoSbJwe))) Begin
        Send CreateComObject of hoSbJwe
    End
    Get ComLoadFile Of hoSbJwe "qa_data/message.jwe" "utf-8" To iBLoaded
    If (iBLoaded <> True) Begin
        Showln "Failed to load the JWE file."
        Procedure_Return
    End

    //  Load the JWE from the StringBuilder.
    Get pvComObject of hoSbJwe to vSbJwe
    Get ComLoadJweSb Of hoJwe vSbJwe 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

    Get ComDecrypt Of hoJwe 0 "utf-8" To sContent
    Get ComLastMethodSuccess Of hoJwe To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoJwe To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln sContent


End_Procedure