Visual FoxPro
Visual FoxPro
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loSbPlainText
LOCAL lnBCrLf
LOCAL lcLine
LOCAL loJwe
LOCAL loJweProtHdr
LOCAL lcAesWrappingKey
LOCAL loSbJweCompressed
LOCAL loSbJweUncompressed
LOCAL loJwe2
LOCAL loSbOriginalText
lnSuccess = 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.
* Create some plaintext to be encrypted.
* This example will demonstrate with and without DEFLATE (zip) compression.
loSbPlainText = CreateObject('Chilkat.StringBuilder')
lnBCrLf = 1
lcLine = "Live long and prosper."
loSbPlainText.AppendLine(lcLine,lnBCrLf)
loSbPlainText.AppendLine(lcLine,lnBCrLf)
loSbPlainText.AppendLine(lcLine,lnBCrLf)
loSbPlainText.AppendLine(lcLine,lnBCrLf)
* The text to be encrypted:
? loSbPlainText.GetAsString()
loJwe = CreateObject('Chilkat.Jwe')
* 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('Chilkat.JsonObject')
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('Chilkat.StringBuilder')
lnSuccess = loJwe.EncryptSb(loSbPlainText,"utf-8",loSbJweCompressed)
IF (lnSuccess <> 1) THEN
? loJwe.LastErrorText
RELEASE loSbPlainText
RELEASE loJwe
RELEASE loJweProtHdr
RELEASE loSbJweCompressed
CANCEL
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('Chilkat.StringBuilder')
lnSuccess = loJwe.EncryptSb(loSbPlainText,"utf-8",loSbJweUncompressed)
IF (lnSuccess <> 1) THEN
? loJwe.LastErrorText
RELEASE loSbPlainText
RELEASE loJwe
RELEASE loJweProtHdr
RELEASE loSbJweCompressed
RELEASE loSbJweUncompressed
CANCEL
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('Chilkat.Jwe')
lnSuccess = loJwe2.LoadJweSb(loSbJweCompressed)
IF (lnSuccess <> 1) THEN
? loJwe2.LastErrorText
RELEASE loSbPlainText
RELEASE loJwe
RELEASE loJweProtHdr
RELEASE loSbJweCompressed
RELEASE loSbJweUncompressed
RELEASE loJwe2
CANCEL
ENDIF
* Set the AES wrap key.
loJwe2.SetWrappingKey(0,lcAesWrappingKey,"base64url")
* Decrypt (also automatically decompresses).
loSbOriginalText = CreateObject('Chilkat.StringBuilder')
lnSuccess = loJwe2.DecryptSb(0,"utf-8",loSbOriginalText)
IF (lnSuccess <> 1) THEN
? loJwe2.LastErrorText
RELEASE loSbPlainText
RELEASE loJwe
RELEASE loJweProtHdr
RELEASE loSbJweCompressed
RELEASE loSbJweUncompressed
RELEASE loJwe2
RELEASE loSbOriginalText
CANCEL
ENDIF
? "original text from compressed JWE: "
? loSbOriginalText.GetAsString()
* -----------------------------------------------------------
* Do the same with the uncompressed JWE
lnSuccess = loJwe2.LoadJweSb(loSbJweUncompressed)
IF (lnSuccess <> 1) THEN
? loJwe2.LastErrorText
RELEASE loSbPlainText
RELEASE loJwe
RELEASE loJweProtHdr
RELEASE loSbJweCompressed
RELEASE loSbJweUncompressed
RELEASE loJwe2
RELEASE loSbOriginalText
CANCEL
ENDIF
* Set the AES wrap key.
loJwe2.SetWrappingKey(0,lcAesWrappingKey,"base64url")
* Decrypt.
loSbOriginalText.Clear()
lnSuccess = loJwe2.DecryptSb(0,"utf-8",loSbOriginalText)
IF (lnSuccess <> 1) THEN
? loJwe2.LastErrorText
RELEASE loSbPlainText
RELEASE loJwe
RELEASE loJweProtHdr
RELEASE loSbJweCompressed
RELEASE loSbJweUncompressed
RELEASE loJwe2
RELEASE loSbOriginalText
CANCEL
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