Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Visual Basic 6.0 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
Box
CAdES
CSR
CSV
Cert Store
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
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
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Visual Basic 6.0) Example: Crypt2.RandomizeIV method

Demonstrates using a random initialization vector for AES GCM encryption.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

' This example assumes the Chilkat API to have been previously unlocked.
' See Global Unlock Sample for sample code.

Dim crypt As New ChilkatCrypt2

crypt.CryptAlgorithm = "aes"
crypt.CipherMode = "gcm"
crypt.KeyLength = 256

Dim K As String
K = "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F"
Dim AAD As String
AAD = "feedfacedeadbeeffeedfacedeadbeefabaddad2"
Dim PT As String
PT = "This is the text to be AES-GCM encrypted."

' Generate a random IV.
crypt.RandomizeIV 
Dim IV As String
IV = crypt.GetEncodedIV("hex")

crypt.SetEncodedKey K,"hex"

Dim success As Long
success = crypt.SetEncodedAad(AAD,"hex")

' Return the encrypted bytes as base64
crypt.EncodingMode = "base64"
crypt.Charset = "utf-8"
Dim cipherText As String
cipherText = crypt.EncryptStringENC(PT)
If (crypt.LastMethodSuccess <> 1) Then
    Debug.Print crypt.LastErrorText
    Exit Sub
End If

' Get the GCM authenticated tag computed when encrypting.
Dim authTag As String
authTag = crypt.GetEncodedAuthTag("base64")

Debug.Print "Cipher Text: " & cipherText
Debug.Print "Auth Tag: " & authTag

' Let's send the IV, CipherText, and AuthTag to the decrypting party.
' We'll send them concatenated like this: [IV || Ciphertext || AuthTag]
' In base64 format.
Dim bdEncrypted As New ChilkatBinData
success = bdEncrypted.AppendEncoded(IV,"hex")
success = bdEncrypted.AppendEncoded(cipherText,"base64")
success = bdEncrypted.AppendEncoded(authTag,"base64")

Dim concatenatedGcmOutput As String
concatenatedGcmOutput = bdEncrypted.GetEncoded("base64")
Debug.Print "Concatenated GCM Output: " & concatenatedGcmOutput

' Sample output so far:

' -------------------------------------------------------------------------------------
' Now let's GCM decrypt...
' -------------------------------------------------------------------------------------

Dim decrypt As New ChilkatCrypt2

' The values shared and agreed upon by both sides beforehand are: algorithm, cipher mode, secret key, and AAD.
' Sometimes the IV can be a value already known and agreed upon, but in this case the encryptor sends the IV to the decryptor.
decrypt.CryptAlgorithm = "aes"
decrypt.CipherMode = "gcm"
decrypt.KeyLength = 256
decrypt.SetEncodedKey K,"hex"
success = decrypt.SetEncodedAad(AAD,"hex")

Dim bdFromEncryptor As New ChilkatBinData
success = bdFromEncryptor.AppendEncoded(concatenatedGcmOutput,"base64")

Dim sz As Long
sz = bdFromEncryptor.NumBytes

' Extract the parts.
Dim extractedIV As String
extractedIV = bdFromEncryptor.GetEncodedChunk(0,16,"hex")
Dim extractedCipherText As String
extractedCipherText = bdFromEncryptor.GetEncodedChunk(16,sz - 32,"base64")
Dim expectedAuthTag As String
expectedAuthTag = bdFromEncryptor.GetEncodedChunk(sz - 16,16,"base64")

' Before GCM decrypting, we must set the authenticated tag to the value that is expected.
' The decryption will fail if the resulting authenticated tag is not equal to the expected result.
success = decrypt.SetEncodedAuthTag(expectedAuthTag,"base64")

' Also set the IV.
decrypt.SetEncodedIV extractedIV,"hex"

' Decrypt..
decrypt.EncodingMode = "base64"
decrypt.Charset = "utf-8"
Dim decryptedText As String
decryptedText = decrypt.DecryptStringENC(extractedCipherText)
If (decrypt.LastMethodSuccess <> 1) Then
    ' Failed.  The resultant authenticated tag did not equal the expected authentication tag.
    Debug.Print decrypt.LastErrorText
    Exit Sub
End If

Debug.Print "Decrypted: " & decryptedText

 

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