Lianja
Lianja
JWE with DEFLATE Compression
See more JSON Web Encryption (JWE) Examples
Demonstrates how to DEFLATE ("zip") compress the JWE payload prior to encryption.Note: This example requires Chilkat v9.5.0.66 or greater.
Chilkat Lianja Downloads
llSuccess = .F.
// 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.
// Create some plaintext to be encrypted.
// This example will demonstrate with and without DEFLATE (zip) compression.
loSbPlainText = createobject("CkStringBuilder")
llBCrLf = .T.
lcLine = "Live long and prosper."
loSbPlainText.AppendLine(lcLine,llBCrLf)
loSbPlainText.AppendLine(lcLine,llBCrLf)
loSbPlainText.AppendLine(lcLine,llBCrLf)
loSbPlainText.AppendLine(lcLine,llBCrLf)
// The text to be encrypted:
? loSbPlainText.GetAsString()
loJwe = createobject("CkJwe")
// Build the JWE Protected Header: {"alg":"A128KW","enc":"A128CBC-HS256","zip":"DEF"}
// The "zip":"DEF" parameter indicates that the plaintext payload should
// be compressed prior to encryption.
loJweProtHdr = createobject("CkJsonObject")
loJweProtHdr.AppendString("alg","A128KW")
loJweProtHdr.AppendString("enc","A128CBC-HS256")
loJweProtHdr.AppendString("zip","DEF")
loJwe.SetProtectedHeader(loJweProtHdr)
// Set the AES key wrap key:
lcAesWrappingKey = "GawgguFyGrWKav7AX4VKUg"
loJwe.SetWrappingKey(0,lcAesWrappingKey,"base64url")
// Encrypt and return the JWE in sbJweCompressed:
loSbJweCompressed = createobject("CkStringBuilder")
llSuccess = loJwe.EncryptSb(loSbPlainText,"utf-8",loSbJweCompressed)
if (llSuccess <> .T.) then
? loJwe.LastErrorText
release loSbPlainText
release loJwe
release loJweProtHdr
release loSbJweCompressed
return
endif
// Show the compressed JWE:
? loSbJweCompressed.GetAsString()
? "size of compressed JWE: " + str(loSbJweCompressed.Length)
// Now create a JWE without compression.
loJweProtHdr.Delete("zip")
// Make sure to update the shared protected header:
loJwe.SetProtectedHeader(loJweProtHdr)
loSbJweUncompressed = createobject("CkStringBuilder")
llSuccess = loJwe.EncryptSb(loSbPlainText,"utf-8",loSbJweUncompressed)
if (llSuccess <> .T.) then
? loJwe.LastErrorText
release loSbPlainText
release loJwe
release loJweProtHdr
release loSbJweCompressed
release loSbJweUncompressed
return
endif
// Show the uncompressed JWE:
? loSbJweUncompressed.GetAsString()
? "size of uncompressed JWE: " + str(loSbJweUncompressed.Length)
// Decrypting is the same whether compression is used or not.
// The "zip" header in the JWE indicates that the payload should be
// automatically decompressed (inflated) after decrypting.
loJwe2 = createobject("CkJwe")
llSuccess = loJwe2.LoadJweSb(loSbJweCompressed)
if (llSuccess <> .T.) then
? loJwe2.LastErrorText
release loSbPlainText
release loJwe
release loJweProtHdr
release loSbJweCompressed
release loSbJweUncompressed
release loJwe2
return
endif
// Set the AES wrap key.
loJwe2.SetWrappingKey(0,lcAesWrappingKey,"base64url")
// Decrypt (also automatically decompresses).
loSbOriginalText = createobject("CkStringBuilder")
llSuccess = loJwe2.DecryptSb(0,"utf-8",loSbOriginalText)
if (llSuccess <> .T.) then
? loJwe2.LastErrorText
release loSbPlainText
release loJwe
release loJweProtHdr
release loSbJweCompressed
release loSbJweUncompressed
release loJwe2
release loSbOriginalText
return
endif
? "original text from compressed JWE: "
? loSbOriginalText.GetAsString()
// -----------------------------------------------------------
// Do the same with the uncompressed JWE
llSuccess = loJwe2.LoadJweSb(loSbJweUncompressed)
if (llSuccess <> .T.) then
? loJwe2.LastErrorText
release loSbPlainText
release loJwe
release loJweProtHdr
release loSbJweCompressed
release loSbJweUncompressed
release loJwe2
release loSbOriginalText
return
endif
// Set the AES wrap key.
loJwe2.SetWrappingKey(0,lcAesWrappingKey,"base64url")
// Decrypt.
loSbOriginalText.Clear()
llSuccess = loJwe2.DecryptSb(0,"utf-8",loSbOriginalText)
if (llSuccess <> .T.) then
? loJwe2.LastErrorText
release loSbPlainText
release loJwe
release loJweProtHdr
release loSbJweCompressed
release loSbJweUncompressed
release loJwe2
release loSbOriginalText
return
endif
? "original text from uncompressed JWE: "
? loSbOriginalText.GetAsString()
// ------------------------------------------------
// The output of this example is:
// (Note: Your output data will be different because the content encryption key is randomly generated.)
// eyJhbGciOiJBMTI4S1ciLCJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiemlwIjoiREVGIn0.xuW-pEAIdEUFnk10m8ocursvktO8Of9ByCCAt6LgKkkOtCWCUn1kQw.zpGj-9WVni3cQxyOuZbcGA.0hzP1myua3oYpUHwCIY_3edBUREbUpLaX6wYuJduOdI.Ppc6aEO3y3B8BJ1FKMPjlA
// size of compressed JWE: 212
// eyJhbGciOiJBMTI4S1ciLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0.N4KeyC7nnSFkieJOyE24_zKeuV_m7v5UKoJb1TgV4Yc_r2RzUPNvyA.6AEdyXSCKx-iMmUJyypSLg.QpixfyrwhGpmwUDp623viik4smPav7vwPLiC2r-V-jwnSfEH3mxWu6DbrIz3mixaqATwynmEBzVPxvS9jTXpSAGCnniib4_0WoPl3r_wF5tlsKOEe--jpNso-DKd1Tp8jJxj3JkFWt3IRnUUKGj17g.sBfDwFc5fzpaI-UW8-SW4g
// size of uncompressed JWE: 303
// original text from compressed JWE:
// Live long and prosper.
// Live long and prosper.
// Live long and prosper.
// Live long and prosper.
//
// original text from uncompressed JWE:
// Live long and prosper.
// Live long and prosper.
// Live long and prosper.
// Live long and prosper.
//
release loSbPlainText
release loJwe
release loJweProtHdr
release loSbJweCompressed
release loSbJweUncompressed
release loJwe2
release loSbOriginalText