Sample code for 30+ languages & platforms
Lianja

Set JWE Additional Authenticated Data from BinData

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.SetAadBd, which sets external Additional Authenticated Data (AAD) to the exact bytes held in a BinData.

Background. AAD is integrity-protected but not encrypted. Passing an empty BinData clears any previously configured AAD.

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 external Additional Authenticated Data (AAD) to the exact bytes in a BinData.  Passing an empty
//  BinData clears any previously configured AAD.
loBdAad = createobject("CkBinData")
loBdAad.AppendString("context-info-v1","utf-8")
llSuccess = loJwe.SetAadBd(loBdAad)
if (llSuccess = .F.) then
    ? loJwe.LastErrorText
    release loJwe
    release loHeader
    release loBdAad
    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 loBdAad
    release loSbContent
    release loSbJwe
    return
endif

? loSbJwe.GetAsString()


release loJwe
release loHeader
release loBdAad
release loSbContent
release loSbJwe