Sample code for 30+ languages & platforms
Lianja

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

Lianja
llSuccess = .F.

loJwe = createobject("CkJwe")

//  Protected header: AES key-wrap with AES-256-GCM content encryption.
loHeader = createobject("CkJsonObject")
loHeader.UpdateString("alg","A256KW")
loHeader.UpdateString("enc","A256GCM")
llSuccess = loJwe.SetProtectedHeader(loHeader)
if (llSuccess = .F.) then
    ? loJwe.LastErrorText
    release loJwe
    release loHeader
    return
endif

//  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.
lcBase64Key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU="
llSuccess = loJwe.SetWrappingKey(0,lcBase64Key,"base64")
if (llSuccess = .F.) then
    ? loJwe.LastErrorText
    release loJwe
    release loHeader
    return
endif

//  Set the shared JWE unprotected header.  It is shared by all recipients but, unlike the protected
//  header, is not integrity-protected.
loUnprotected = createobject("CkJsonObject")
loUnprotected.UpdateString("cty","text/plain")
llSuccess = loJwe.SetUnprotectedHeader(loUnprotected)
if (llSuccess = .F.) then
    ? loJwe.LastErrorText
    release loJwe
    release loHeader
    release loUnprotected
    return
endif

loSbContent = createobject("CkStringBuilder")
loSbContent.Append("This is the secret content.")
loSbJwe = createobject("CkStringBuilder")
llSuccess = loJwe.EncryptSb(loSbContent,"utf-8",loSbJwe)
if (llSuccess = .F.) then
    ? loJwe.LastErrorText
    release loJwe
    release loHeader
    release loUnprotected
    release loSbContent
    release loSbJwe
    return
endif

? loSbJwe.GetAsString()


release loJwe
release loHeader
release loUnprotected
release loSbContent
release loSbJwe