Sample code for 30+ languages & platforms
Xbase++

JWE Using Flattened JWE JSON Serialization

See more JSON Web Encryption (JWE) Examples

This example duplicates the example A.5 in RFC 7516 for JSON Web Encryption (JWE).

This example demonstrates using the flattened JWE JSON Serialization syntax. This example demonstrates the capability for encrypting the plaintext to a single recipient in a flattened JSON structure.

Note: This example requires Chilkat v9.5.0.66 or greater.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL cPlaintext
LOCAL oJwe
LOCAL oJweProtHdr
LOCAL oJweRecipientHdr
LOCAL nRecipientIndex
LOCAL oJweUnprotHdr
LOCAL cAesWrappingKey
LOCAL cStrJwe
LOCAL oJsonTemp
LOCAL oJwe2
LOCAL cOriginalPlaintext
LOCAL oSbJwe

nSuccess := 0

//  This requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  Note: This example requires Chilkat v9.5.0.66 or greater.

cPlaintext := "Live long and prosper."

oJwe := CreateObject("Chilkat.Jwe")

//  First build the JWE Protected Header: {"enc":"A128CBC-HS256"}
oJweProtHdr := CreateObject("Chilkat.JsonObject")
oJweProtHdr:AppendString("enc", "A128CBC-HS256")
oJwe:SetProtectedHeader(oJweProtHdr)

//  We have a single recipient that uses AES Key Wrap to encrypt the CEK.
//  
//       {"alg":"A128KW","kid":"7"}

oJweRecipientHdr := CreateObject("Chilkat.JsonObject")
oJweRecipientHdr:AppendString("alg", "A128KW")
oJweRecipientHdr:AppendString("kid", "7")
nRecipientIndex := 0
oJwe:SetRecipientHeader(nRecipientIndex, oJweRecipientHdr)

//  Set the Shared Unprotected Header:  {"jku":"https://server.example.com/keys.jwks"}
oJweUnprotHdr := CreateObject("Chilkat.JsonObject")
oJweUnprotHdr:AppendString("jku", "https://server.example.com/keys.jwks")
oJwe:SetUnprotectedHeader(oJweUnprotHdr)

//  Set the AES key wrapping key.
cAesWrappingKey := "GawgguFyGrWKav7AX4VKUg"
nRecipientIndex := 0
oJwe:SetWrappingKey(nRecipientIndex, cAesWrappingKey, "base64url")

//  OK.. everything has been specified.
//  Now encrypt.  
//  Indicate that we prefer the flattened JSON serialization.
oJwe:PreferFlattened := 1
cStrJwe := oJwe:Encrypt(cPlaintext, "utf-8")
IF (oJwe:LastMethodSuccess != 1)
    ? oJwe:LastErrorText
    oJwe:destroy()
    oJweProtHdr:destroy()
    oJweRecipientHdr:destroy()
    oJweUnprotHdr:destroy()
    RETURN
ENDIF

//  The JWE is produced in the most compact form possible (a single line).
//  Let's load it into a JSON object and examine in a non-compact pretty-printed format:
oJsonTemp := CreateObject("Chilkat.JsonObject")
oJsonTemp:Load(cStrJwe)
oJsonTemp:EmitCompact := 0
? oJsonTemp:Emit()

//  The JWE looks like this:
//  (Note: Because of random values used in the encryption process, your encrypted results will be different.)

//  	{ 
//  	  "protected": "eyJlbmMiOiJBMTI4Q0JDLUhTMjU2In0",
//  	  "unprotected": { 
//  	    "jku": "https://server.example.com/keys.jwks"
//  	  },
//  	  "header": { 
//  	    "alg": "A128KW",
//  	    "kid": "7"
//  	  },
//  	  "encrypted_key": "FW7z9VxOvnF2ClicvUT4HTeqcdyh90C6qytfEFJsOb77FOwTfYrc3w",
//  	  "iv": "O6Ly5-eML3zTs-_fNX3D5Q",
//  	  "ciphertext": "Q8NLmeac9JhLh0CQPuG_7D9wVDb55eToCh0g5-FQ4M0",
//  	  "tag": "slzlo6-J9C7wSDF4dh_kBg"
//  	} 

//  Now decrypt......................................

//  First, load the JWE..
oJwe2 := CreateObject("Chilkat.Jwe")
nSuccess := oJwe2:LoadJwe(cStrJwe)
IF (nSuccess != 1)
    ? oJwe2:LastErrorText
    oJwe:destroy()
    oJweProtHdr:destroy()
    oJweRecipientHdr:destroy()
    oJweUnprotHdr:destroy()
    oJsonTemp:destroy()
    oJwe2:destroy()
    RETURN
ENDIF

//  Set the AES wrap key..
nRecipientIndex := 0
oJwe2:SetWrappingKey(nRecipientIndex, cAesWrappingKey, "base64url")

//  Decrypt
cOriginalPlaintext := oJwe2:Decrypt(nRecipientIndex, "utf-8")
IF (oJwe2:LastMethodSuccess != 1)
    ? oJwe2:LastErrorText
    oJwe:destroy()
    oJweProtHdr:destroy()
    oJweRecipientHdr:destroy()
    oJweUnprotHdr:destroy()
    oJsonTemp:destroy()
    oJwe2:destroy()
    RETURN
ENDIF

? "original text decrypted with AES key wrap key: "
? cOriginalPlaintext

//  ---------------------------------------------------------------------------------
//  It should also be possible to decrypt the flattened JWE as shown in RFC 7516, Appendix A.5
//  because it was produced using the same key.

oSbJwe := CreateObject("Chilkat.StringBuilder")

oSbJwe:Append("{")
oSbJwe:Append('"protected":')
oSbJwe:Append('"eyJlbmMiOiJBMTI4Q0JDLUhTMjU2In0",')
oSbJwe:Append('"unprotected":')
oSbJwe:Append('{"jku":"https://server.example.com/keys.jwks"},')
oSbJwe:Append('"header":')
oSbJwe:Append('{"alg":"A128KW","kid":"7"},')
oSbJwe:Append('"encrypted_key":')
oSbJwe:Append('"6KB707dM9YTIgHtLvtgWQ8mKwboJW3of9locizkDTHzBC2IlrT1oOQ",')
oSbJwe:Append('"iv":')
oSbJwe:Append('"AxY8DCtDaGlsbGljb3RoZQ",')
oSbJwe:Append('"ciphertext":')
oSbJwe:Append('"KDlTtXchhZTGufMYmOYGS4HffxPSUrfmqCHXaI9wOGY",')
oSbJwe:Append('"tag":')
oSbJwe:Append('"Mz-VPPyU4RlcuYv1IwIvzw"')
oSbJwe:Append("}")

nSuccess := oJwe2:LoadJweSb(oSbJwe)
IF (nSuccess != 1)
    ? oJwe2:LastErrorText
    oJwe:destroy()
    oJweProtHdr:destroy()
    oJweRecipientHdr:destroy()
    oJweUnprotHdr:destroy()
    oJsonTemp:destroy()
    oJwe2:destroy()
    oSbJwe:destroy()
    RETURN
ENDIF

nRecipientIndex := 0
oJwe2:SetWrappingKey(nRecipientIndex, cAesWrappingKey, "base64url")

cOriginalPlaintext := oJwe2:Decrypt(nRecipientIndex, "utf-8")
IF (oJwe2:LastMethodSuccess != 1)
    ? oJwe2:LastErrorText
    oJwe:destroy()
    oJweProtHdr:destroy()
    oJweRecipientHdr:destroy()
    oJweUnprotHdr:destroy()
    oJsonTemp:destroy()
    oJwe2:destroy()
    oSbJwe:destroy()
    RETURN
ENDIF

? "original text decrypted from published flattened JWE: "
? cOriginalPlaintext

//  The output:

//  original text decrypted with AES key wrap key: 
//  Live long and prosper.
//  original text decrypted from published flattened JWE: 
//  Live long and prosper.

oJwe:destroy()
oJweProtHdr:destroy()
oJweRecipientHdr:destroy()
oJweUnprotHdr:destroy()
oJsonTemp:destroy()
oJwe2:destroy()
oSbJwe:destroy()