Sample code for 30+ languages & platforms
PureBasic

Set the JWE Protected Header

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.SetProtectedHeader, which sets the integrity-protected JWE header from a JsonObject. The header specifies the key-management algorithm (alg) and content-encryption algorithm (enc).

Background. The protected header is integrity-protected and applies to all recipients. It is configured before encrypting.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkJwe.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    jwe.i = CkJwe::ckCreate()
    If jwe.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Build the JWE protected header.  The "alg" member names the key-management algorithm and the "enc"
    ;  member names the content-encryption algorithm.
    header.i = CkJsonObject::ckCreate()
    If header.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(header,"alg","A256KW")
    CkJsonObject::ckUpdateString(header,"enc","A256GCM")

    ;  Set the protected header on the JWE.  The protected header is integrity-protected and applies to
    ;  all recipients.
    success = CkJwe::ckSetProtectedHeader(jwe,header)
    If success = 0
        Debug CkJwe::ckLastErrorText(jwe)
        CkJwe::ckDispose(jwe)
        CkJsonObject::ckDispose(header)
        ProcedureReturn
    EndIf

    Debug "Protected header set."


    CkJwe::ckDispose(jwe)
    CkJsonObject::ckDispose(header)


    ProcedureReturn
EndProcedure