Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
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
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) JWE using ECDH-ES, BP-256, A256GCM

See more JSON Web Encryption (JWE) Examples

Create a JWE with the following header:

	{
	  "alg": "ECDH-ES",
	  "enc": "A256GCM",
	  "exp": 1621957030,
	  "cty": "NJWT",
	  "epk": {
	    "kty": "EC",
	    "x": "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w"
	    "y": "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"
	    "crv": "BP-256"
	  }
	}

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

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJson
    Variant vPubkey
    Handle hoPubkey
    Handle hoJwt
    Variant vJweProtHdr
    Handle hoJweProtHdr
    Handle hoJwe
    String sPlainText
    String sStrJwe
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

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

    // Load our brainpool BP-256 public key.

    // 	{
    // 	  "use": "enc",
    // 	  "kid": "puk_idp_enc",
    // 	  "kty": "EC",
    // 	  "crv": "BP-256",
    // 	  "x": "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w",
    // 	  "y": "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"
    // 	}

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "use" "enc" To iSuccess
    Get ComUpdateString Of hoJson "kid" "puk_idp_enc" To iSuccess
    Get ComUpdateString Of hoJson "kty" "EC" To iSuccess
    Get ComUpdateString Of hoJson "crv" "BP-256" To iSuccess
    Get ComUpdateString Of hoJson "x" "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w" To iSuccess
    Get ComUpdateString Of hoJson "y" "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu" To iSuccess

    Get Create (RefClass(cComChilkatPublicKey)) To hoPubkey
    If (Not(IsComObjectCreated(hoPubkey))) Begin
        Send CreateComObject of hoPubkey
    End

    Get ComEmit Of hoJson To sTemp1
    Get ComLoadFromString Of hoPubkey sTemp1 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPubkey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Build our protected header:

    // 	{
    // 	  "alg": "ECDH-ES",
    // 	  "enc": "A256GCM",
    // 	  "exp": 1621957030,
    // 	  "cty": "NJWT",
    // 	  "epk": {
    // 	    "kty": "EC",
    // 	    "x": "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w"
    // 	    "y": "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"
    // 	    "crv": "BP-256"
    // 	  }
    // 	}

    // Use jwt only for getting the current date/time + 3600 seconds.
    Get Create (RefClass(cComChilkatJwt)) To hoJwt
    If (Not(IsComObjectCreated(hoJwt))) Begin
        Send CreateComObject of hoJwt
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJweProtHdr
    If (Not(IsComObjectCreated(hoJweProtHdr))) Begin
        Send CreateComObject of hoJweProtHdr
    End
    Get ComUpdateString Of hoJweProtHdr "alg" "ECDH-ES" To iSuccess
    Get ComUpdateString Of hoJweProtHdr "enc" "A256GCM" To iSuccess
    Get ComGenNumericDate Of hoJwt 3600 To iTemp1
    Get ComUpdateInt Of hoJweProtHdr "exp" iTemp1 To iSuccess
    Get ComUpdateString Of hoJweProtHdr "cty" "NJWT" To iSuccess
    Get ComUpdateString Of hoJweProtHdr "epk.kty" "EC" To iSuccess
    Get ComUpdateString Of hoJweProtHdr "epk.x" "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w" To iSuccess
    Get ComUpdateString Of hoJweProtHdr "epk.y" "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu" To iSuccess
    Get ComUpdateString Of hoJweProtHdr "epk.crv" "BP-256" To iSuccess

    Get Create (RefClass(cComChilkatJwe)) To hoJwe
    If (Not(IsComObjectCreated(hoJwe))) Begin
        Send CreateComObject of hoJwe
    End
    Get pvComObject of hoJweProtHdr to vJweProtHdr
    Get ComSetProtectedHeader Of hoJwe vJweProtHdr To iSuccess
    Get pvComObject of hoPubkey to vPubkey
    Get ComSetPublicKey Of hoJwe 0 vPubkey To iSuccess

    Move "This is the text to be encrypted." To sPlainText
    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

    Showln sStrJwe

    Showln "Success."


End_Procedure

 

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