Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJwe.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 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.
    sbPlainText.i = CkStringBuilder::ckCreate()
    If sbPlainText.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    bCrLf.i = 1
    line.s = "Live long and prosper."
    CkStringBuilder::ckAppendLine(sbPlainText,line,bCrLf)
    CkStringBuilder::ckAppendLine(sbPlainText,line,bCrLf)
    CkStringBuilder::ckAppendLine(sbPlainText,line,bCrLf)
    CkStringBuilder::ckAppendLine(sbPlainText,line,bCrLf)

    ; The text to be encrypted:
    Debug CkStringBuilder::ckGetAsString(sbPlainText)

    jwe.i = CkJwe::ckCreate()
    If jwe.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; 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.
    jweProtHdr.i = CkJsonObject::ckCreate()
    If jweProtHdr.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckAppendString(jweProtHdr,"alg","A128KW")
    CkJsonObject::ckAppendString(jweProtHdr,"enc","A128CBC-HS256")
    CkJsonObject::ckAppendString(jweProtHdr,"zip","DEF")
    CkJwe::ckSetProtectedHeader(jwe,jweProtHdr)

    ; Set the AES key wrap key:
    aesWrappingKey.s = "GawgguFyGrWKav7AX4VKUg"
    CkJwe::ckSetWrappingKey(jwe,0,aesWrappingKey,"base64url")

    ; Encrypt and return the JWE in sbJweCompressed:
    sbJweCompressed.i = CkStringBuilder::ckCreate()
    If sbJweCompressed.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJwe::ckEncryptSb(jwe,sbPlainText,"utf-8",sbJweCompressed)
    If success <> 1
        Debug CkJwe::ckLastErrorText(jwe)
        CkStringBuilder::ckDispose(sbPlainText)
        CkJwe::ckDispose(jwe)
        CkJsonObject::ckDispose(jweProtHdr)
        CkStringBuilder::ckDispose(sbJweCompressed)
        ProcedureReturn
    EndIf

    ; Show the compressed JWE:
    Debug CkStringBuilder::ckGetAsString(sbJweCompressed)
    Debug "size of compressed JWE: " + Str(CkStringBuilder::ckLength(sbJweCompressed))

    ; Now create a JWE without compression.
    CkJsonObject::ckDelete(jweProtHdr,"zip")
    ; Make sure to update the shared protected header:
    CkJwe::ckSetProtectedHeader(jwe,jweProtHdr)

    sbJweUncompressed.i = CkStringBuilder::ckCreate()
    If sbJweUncompressed.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJwe::ckEncryptSb(jwe,sbPlainText,"utf-8",sbJweUncompressed)
    If success <> 1
        Debug CkJwe::ckLastErrorText(jwe)
        CkStringBuilder::ckDispose(sbPlainText)
        CkJwe::ckDispose(jwe)
        CkJsonObject::ckDispose(jweProtHdr)
        CkStringBuilder::ckDispose(sbJweCompressed)
        CkStringBuilder::ckDispose(sbJweUncompressed)
        ProcedureReturn
    EndIf

    ; Show the uncompressed JWE:
    Debug CkStringBuilder::ckGetAsString(sbJweUncompressed)
    Debug "size of uncompressed JWE: " + Str(CkStringBuilder::ckLength(sbJweUncompressed))

    ; 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.
    jwe2.i = CkJwe::ckCreate()
    If jwe2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJwe::ckLoadJweSb(jwe2,sbJweCompressed)
    If success <> 1
        Debug CkJwe::ckLastErrorText(jwe2)
        CkStringBuilder::ckDispose(sbPlainText)
        CkJwe::ckDispose(jwe)
        CkJsonObject::ckDispose(jweProtHdr)
        CkStringBuilder::ckDispose(sbJweCompressed)
        CkStringBuilder::ckDispose(sbJweUncompressed)
        CkJwe::ckDispose(jwe2)
        ProcedureReturn
    EndIf

    ; Set the AES wrap key.
    CkJwe::ckSetWrappingKey(jwe2,0,aesWrappingKey,"base64url")

    ; Decrypt (also automatically decompresses).
    sbOriginalText.i = CkStringBuilder::ckCreate()
    If sbOriginalText.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJwe::ckDecryptSb(jwe2,0,"utf-8",sbOriginalText)
    If success <> 1
        Debug CkJwe::ckLastErrorText(jwe2)
        CkStringBuilder::ckDispose(sbPlainText)
        CkJwe::ckDispose(jwe)
        CkJsonObject::ckDispose(jweProtHdr)
        CkStringBuilder::ckDispose(sbJweCompressed)
        CkStringBuilder::ckDispose(sbJweUncompressed)
        CkJwe::ckDispose(jwe2)
        CkStringBuilder::ckDispose(sbOriginalText)
        ProcedureReturn
    EndIf

    Debug "original text from compressed JWE: "
    Debug CkStringBuilder::ckGetAsString(sbOriginalText)

    ; -----------------------------------------------------------
    ; Do the same with the uncompressed JWE

    success = CkJwe::ckLoadJweSb(jwe2,sbJweUncompressed)
    If success <> 1
        Debug CkJwe::ckLastErrorText(jwe2)
        CkStringBuilder::ckDispose(sbPlainText)
        CkJwe::ckDispose(jwe)
        CkJsonObject::ckDispose(jweProtHdr)
        CkStringBuilder::ckDispose(sbJweCompressed)
        CkStringBuilder::ckDispose(sbJweUncompressed)
        CkJwe::ckDispose(jwe2)
        CkStringBuilder::ckDispose(sbOriginalText)
        ProcedureReturn
    EndIf

    ; Set the AES wrap key.
    CkJwe::ckSetWrappingKey(jwe2,0,aesWrappingKey,"base64url")

    ; Decrypt.
    CkStringBuilder::ckClear(sbOriginalText)
    success = CkJwe::ckDecryptSb(jwe2,0,"utf-8",sbOriginalText)
    If success <> 1
        Debug CkJwe::ckLastErrorText(jwe2)
        CkStringBuilder::ckDispose(sbPlainText)
        CkJwe::ckDispose(jwe)
        CkJsonObject::ckDispose(jweProtHdr)
        CkStringBuilder::ckDispose(sbJweCompressed)
        CkStringBuilder::ckDispose(sbJweUncompressed)
        CkJwe::ckDispose(jwe2)
        CkStringBuilder::ckDispose(sbOriginalText)
        ProcedureReturn
    EndIf

    Debug "original text from uncompressed JWE: "
    Debug CkStringBuilder::ckGetAsString(sbOriginalText)

    ; ------------------------------------------------
    ; 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.
    ; 


    CkStringBuilder::ckDispose(sbPlainText)
    CkJwe::ckDispose(jwe)
    CkJsonObject::ckDispose(jweProtHdr)
    CkStringBuilder::ckDispose(sbJweCompressed)
    CkStringBuilder::ckDispose(sbJweUncompressed)
    CkJwe::ckDispose(jwe2)
    CkStringBuilder::ckDispose(sbOriginalText)


    ProcedureReturn
EndProcedure