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

PowerBuilder 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
Secrets
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

 

 

 

(PowerBuilder) AES GCM Encrypt and Decrypt a File

See more Encryption Examples

Demonstrates how to AES GCM encrypt and decrypt a file.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

integer li_rc
oleobject loo_Crypt
string K
string ls_IV
string ls_AAD
integer li_Success
string ls_InFile
string ls_OutFile
string ls_AuthTag

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

loo_Crypt = create oleobject
// Use "Chilkat_9_5_0.Crypt2" for versions of Chilkat < 10.0.0
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")
if li_rc < 0 then
    destroy loo_Crypt
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Set the encryption algorithm to "AES"	
loo_Crypt.CryptAlgorithm = "aes"

// Indicate that the Galois/Counter Mode (GCM) should be used:
loo_Crypt.CipherMode = "gcm"

// KeyLength may be 128, 192, 256
loo_Crypt.KeyLength = 256

// This is the 256-bit AES secret key (in hex format)
K = "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F"

// This is the 16-byte initialization vector (in hex format)
ls_IV = "000102030405060708090A0B0C0D0E0F"

// This is the OPTIONAL additional data (in hex format) to be used as input to the GCM AEAD algorithm,
// but is not included in the output.  It plays a role in the computation of the
// resulting authenticated tag.
ls_AAD = "feedfacedeadbeeffeedfacedeadbeefabaddad2"

// Set the secret key and IV
loo_Crypt.SetEncodedIV(ls_IV,"hex")
loo_Crypt.SetEncodedKey(K,"hex")

// Set the additional authenticated data (AAD)
li_Success = loo_Crypt.SetEncodedAad(ls_AAD,"hex")

// Encrypt a file.
ls_InFile = "qa_data/hamlet.xml"
ls_OutFile = "c:/temp/qa_output/hamlet_aes_gcm.enc"
li_Success = loo_Crypt.CkEncryptFile(ls_InFile,ls_OutFile)
if li_Success = 0 then
    Write-Debug loo_Crypt.LastErrorText
    destroy loo_Crypt
    return
end if

// Get the authentication tag in hex format
ls_AuthTag = loo_Crypt.GetEncodedAuthTag("hex")
Write-Debug "authentication tag = " + ls_AuthTag

// Decrypt..

// Before decrypting, you must provide the expected authentication tag.
// The decrypt will fail if the resulting authentication tag computed while decrypting is not equal to the
// expected authentication tag.
loo_Crypt.SetEncodedAuthTag(ls_AuthTag,"hex")

ls_InFile = ls_OutFile
ls_OutFile = "c:/temp/qa_output/hamlet_restored.xml"
li_Success = loo_Crypt.CkDecryptFile(ls_InFile,ls_OutFile)
if li_Success = 0 then
    Write-Debug loo_Crypt.LastErrorText
    destroy loo_Crypt
    return
end if

Write-Debug "Success."

// --------------------------------------------------------------------------------------------
// About AES-GCM:

// AES-GCM (Advanced Encryption Standard - Galois/Counter Mode) is a widely-used
// encryption mode that provides both confidentiality (encryption) and
// integrity/authentication (data integrity verification) in one operation. It is
// commonly used in secure communications due to its efficiency and strong security
// properties.
// 
// Key Concepts:
// 
//     AES (Advanced Encryption Standard):
// 
//         AES is a symmetric encryption algorithm, meaning the same key is used
//         for both encryption and decryption.
// 
//         It operates on fixed-size blocks of data (128 bits) using key sizes of
//         128, 192, or 256 bits.
// 
//         In AES-GCM, AES is used to perform the actual data encryption.
// 
//     GCM (Galois/Counter Mode):
// 
//         Counter Mode (CTR): GCM uses counter mode for encryption. In this mode,
//         a nonce (or initialization vector, IV) and a counter are combined and encrypted
//         with AES. The result is XORed with the plaintext to produce the ciphertext. 
// 
//         Galois Mode (GMAC): GCM also includes an authentication mechanism based
//         on a Galois field. It generates an authentication tag, which ensures the
//         integrity of both the ciphertext and any additional data (called AAD -
//         Additional Authenticated Data). This tag is verified during decryption to ensure
//         that the data hasn't been tampered with.
// 
// Key Features:
// 
//     Confidentiality (Encryption):
// 
//         The plaintext is encrypted using AES in counter mode. Each block of
//         plaintext is XORed with the output of AES applied to a combination of the IV and
//         an incremented counter.
// 
//     Integrity (Authentication):
// 
//         In addition to encryption, GCM provides authentication for both the
//         encrypted data (ciphertext) and any Additional Authenticated Data (AAD), such as
//         headers or metadata that need to be protected but not encrypted.
// 
//         The authentication tag is generated using a Galois field multiplication
//         of the ciphertext and AAD. This ensures that any changes to the encrypted
//         message or the AAD will be detected during decryption.
// 
// Key Components:
// 
//     - Plaintext: The data you want to encrypt.
//     - Ciphertext: The encrypted data.
//     - Key: A symmetric key used for both encryption and decryption.
//     - Nonce/IV: A unique value used for each encryption to ensure security. It is not secret but should never be reused with the same key.
//     - AAD (Additional Authenticated Data): Optional data that is not encrypted but needs to be authenticated (e.g., headers).
//     - Authentication Tag: A tag generated to verify the integrity and authenticity of the ciphertext and AAD


destroy loo_Crypt

 

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