Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

DataFlex Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(DataFlex) JWE Using Flattened JWE JSON Serialization

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

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Boolean iSuccess
    String sPlaintext
    Handle hoJwe
ProtHdr    Handle hoJweProtHdr
RecipientHdr    Handle hoJweRecipientHdr
    Integer iRecipientIndex
UnprotHdr    Handle hoJweUnprotHdr
    String sAesWrappingKey
    String sStrJwe
    Handle hoJsonTemp
2    Handle hoJwe2
    String sOriginalPlaintext
    Variant vSbJwe
    Handle hoSbJwe
    String sTemp1
    Boolean bTemp1

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

    Move "Live long and prosper." To sPlaintext

    Get Create (RefClass(cComChilkatJwe)) To hoJwe
    If (Not(IsComObjectCreated(hoJwe))) Begin
        Send CreateComObject of hoJwe
    End

    //  First build the JWE Protected Header: {"enc":"A128CBC-HS256"}
    Get Create (RefClass(cComChilkatJsonObject)) To hoJweProtHdr
    If (Not(IsComObjectCreated(hoJweProtHdr))) Begin
        Send CreateComObject of hoJweProtHdr
    End
    Get ComAppendString Of hoJweProtHdr "enc" "A128CBC-HS256" To iSuccess
    Get pvComObject of hoJweProtHdr to vJweProtHdr
    Get ComSetProtectedHeader Of hoJwe vJweProtHdr To iSuccess

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

    Get Create (RefClass(cComChilkatJsonObject)) To hoJweRecipientHdr
    If (Not(IsComObjectCreated(hoJweRecipientHdr))) Begin
        Send CreateComObject of hoJweRecipientHdr
    End
    Get ComAppendString Of hoJweRecipientHdr "alg" "A128KW" To iSuccess
    Get ComAppendString Of hoJweRecipientHdr "kid" "7" To iSuccess
    Move 0 To iRecipientIndex
    Get pvComObject of hoJweRecipientHdr to vJweRecipientHdr
    Get ComSetRecipientHeader Of hoJwe iRecipientIndex vJweRecipientHdr To iSuccess

    //  Set the Shared Unprotected Header:  {"jku":"https://server.example.com/keys.jwks"}
    Get Create (RefClass(cComChilkatJsonObject)) To hoJweUnprotHdr
    If (Not(IsComObjectCreated(hoJweUnprotHdr))) Begin
        Send CreateComObject of hoJweUnprotHdr
    End
    Get ComAppendString Of hoJweUnprotHdr "jku" "https://server.example.com/keys.jwks" To iSuccess
    Get pvComObject of hoJweUnprotHdr to vJweUnprotHdr
    Get ComSetUnprotectedHeader Of hoJwe vJweUnprotHdr To iSuccess

    //  Set the AES key wrapping key.
    Move "GawgguFyGrWKav7AX4VKUg" To sAesWrappingKey
    Move 0 To iRecipientIndex
    Get ComSetWrappingKey Of hoJwe iRecipientIndex sAesWrappingKey "base64url" To iSuccess

    //  OK.. everything has been specified.
    //  Now encrypt.
    //  Indicate that we prefer the flattened JSON serialization.
    Set ComPreferFlattened Of hoJwe To True
    Get ComEncrypt Of hoJwe sPlaintext "utf-8" To sStrJwe
    Get ComLastMethodSuccess Of hoJwe To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoJwe To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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:
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonTemp
    If (Not(IsComObjectCreated(hoJsonTemp))) Begin
        Send CreateComObject of hoJsonTemp
    End
    Get ComLoad Of hoJsonTemp sStrJwe To iSuccess
    Set ComEmitCompact Of hoJsonTemp To False
    Get ComEmit Of hoJsonTemp To sTemp1
    Showln sTemp1

    //  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..
    Get Create (RefClass(cComChilkatJwe)) To hoJwe2
    If (Not(IsComObjectCreated(hoJwe2))) Begin
        Send CreateComObject of hoJwe2
    End
    Get ComLoadJwe Of hoJwe2 sStrJwe To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoJwe2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Set the AES wrap key..
    Move 0 To iRecipientIndex
    Get ComSetWrappingKey Of hoJwe2 iRecipientIndex sAesWrappingKey "base64url" To iSuccess

    //  Decrypt
    Get ComDecrypt Of hoJwe2 iRecipientIndex "utf-8" To sOriginalPlaintext
    Get ComLastMethodSuccess Of hoJwe2 To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoJwe2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "original text decrypted with AES key wrap key: "
    Showln sOriginalPlaintext

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

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJwe
    If (Not(IsComObjectCreated(hoSbJwe))) Begin
        Send CreateComObject of hoSbJwe
    End

    Get ComAppend Of hoSbJwe "{" To iSuccess
    Get ComAppend Of hoSbJwe '"protected":' To iSuccess
    Get ComAppend Of hoSbJwe '"eyJlbmMiOiJBMTI4Q0JDLUhTMjU2In0",' To iSuccess
    Get ComAppend Of hoSbJwe '"unprotected":' To iSuccess
    Get ComAppend Of hoSbJwe '{"jku":"https://server.example.com/keys.jwks"},' To iSuccess
    Get ComAppend Of hoSbJwe '"header":' To iSuccess
    Get ComAppend Of hoSbJwe '{"alg":"A128KW","kid":"7"},' To iSuccess
    Get ComAppend Of hoSbJwe '"encrypted_key":' To iSuccess
    Get ComAppend Of hoSbJwe '"6KB707dM9YTIgHtLvtgWQ8mKwboJW3of9locizkDTHzBC2IlrT1oOQ",' To iSuccess
    Get ComAppend Of hoSbJwe '"iv":' To iSuccess
    Get ComAppend Of hoSbJwe '"AxY8DCtDaGlsbGljb3RoZQ",' To iSuccess
    Get ComAppend Of hoSbJwe '"ciphertext":' To iSuccess
    Get ComAppend Of hoSbJwe '"KDlTtXchhZTGufMYmOYGS4HffxPSUrfmqCHXaI9wOGY",' To iSuccess
    Get ComAppend Of hoSbJwe '"tag":' To iSuccess
    Get ComAppend Of hoSbJwe '"Mz-VPPyU4RlcuYv1IwIvzw"' To iSuccess
    Get ComAppend Of hoSbJwe "}" To iSuccess

    Get pvComObject of hoSbJwe to vSbJwe
    Get ComLoadJweSb Of hoJwe2 vSbJwe To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoJwe2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Move 0 To iRecipientIndex
    Get ComSetWrappingKey Of hoJwe2 iRecipientIndex sAesWrappingKey "base64url" To iSuccess

    Get ComDecrypt Of hoJwe2 iRecipientIndex "utf-8" To sOriginalPlaintext
    Get ComLastMethodSuccess Of hoJwe2 To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoJwe2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "original text decrypted from published flattened JWE: "
    Showln sOriginalPlaintext

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


End_Procedure

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.