Sample code for 30+ languages & platforms
DataFlex

Set the Shared JWE Unprotected Header

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.SetUnprotectedHeader, which sets the shared JWE unprotected header from a JsonObject.

Background. The unprotected header is shared by all recipients but, unlike the protected header, is not integrity-protected. It is carried in the JSON serialization forms of a JWE.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJwe
    Variant vHeader
    Handle hoHeader
    String sBase64Key
    Variant vUnprotected
    Handle hoUnprotected
    Variant vSbContent
    Handle hoSbContent
    Variant vSbJwe
    Handle hoSbJwe
    String sTemp1

    Move False To iSuccess

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

    //  Protected header: AES key-wrap with AES-256-GCM content encryption.
    Get Create (RefClass(cComChilkatJsonObject)) To hoHeader
    If (Not(IsComObjectCreated(hoHeader))) Begin
        Send CreateComObject of hoHeader
    End
    Get ComUpdateString Of hoHeader "alg" "A256KW" To iSuccess
    Get ComUpdateString Of hoHeader "enc" "A256GCM" To iSuccess
    Get pvComObject of hoHeader to vHeader
    Get ComSetProtectedHeader Of hoJwe vHeader 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

    //  Set the shared JWE unprotected header.  It is shared by all recipients but, unlike the protected
    //  header, is not integrity-protected.
    Get Create (RefClass(cComChilkatJsonObject)) To hoUnprotected
    If (Not(IsComObjectCreated(hoUnprotected))) Begin
        Send CreateComObject of hoUnprotected
    End
    Get ComUpdateString Of hoUnprotected "cty" "text/plain" To iSuccess
    Get pvComObject of hoUnprotected to vUnprotected
    Get ComSetUnprotectedHeader Of hoJwe vUnprotected To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJwe To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbContent
    If (Not(IsComObjectCreated(hoSbContent))) Begin
        Send CreateComObject of hoSbContent
    End
    Get ComAppend Of hoSbContent "This is the secret content." To iSuccess
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJwe
    If (Not(IsComObjectCreated(hoSbJwe))) Begin
        Send CreateComObject of hoSbJwe
    End
    Get pvComObject of hoSbContent to vSbContent
    Get pvComObject of hoSbJwe to vSbJwe
    Get ComEncryptSb Of hoJwe vSbContent "utf-8" vSbJwe To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJwe To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComGetAsString Of hoSbJwe To sTemp1
    Showln sTemp1


End_Procedure