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
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) AEAD AES 128-bit GCM

Demonstrates AES encryption using the Galois/Counter Mode (GCM). GCM is an authenticated encryption mode with "additional data" (often referred to as AEAD). GCM is a cipher mode that can be applied to any symmetric encryption algorithm with a 16-byte block size, such as AES and Twofish. In GCM mode, the block encryption algorithm is transformed into a stream encryption algorithm, and therefore no padding occurs (and the PaddingScheme property does not apply). The "additional data" (known as the AAD) does not get encrypted but plays a role in the computation of the resulting "authenticated tag".

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoCrypt
    String K
    String sIV
    String sAAD
    String sPT
    String sCT
    String T
    Boolean iSuccess
    String sCtResult
    String sTResult
    String sPtResult
    String sTInvalid
    String sTemp1
    Boolean bTemp1

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

    Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End

    // Set the encryption algorithm to "AES"	
    Set ComCryptAlgorithm Of hoCrypt To "aes"

    // Indicate that the Galois/Counter Mode (GCM) should be used:
    Set ComCipherMode Of hoCrypt To "gcm"

    // KeyLength may be 128, 192, 256
    Set ComKeyLength Of hoCrypt To 128

    // This is the 128-bit AES secret key (in hex format)
    Move "feffe9928665731c6d6a8f9467308308" To K

    // This is the 16-byte initialization vector:
    Move "cafebabefacedbaddecaf888" To sIV

    // This is the additional data 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.
    Move "feedfacedeadbeeffeedfacedeadbeefabaddad2" To sAAD

    // The plain-text bytes (in hex format) to be encrypted.
    Move "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39" To sPT

    // The expected cipher text (in hex format)
    Move "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091" To sCT

    // The expected authenticated tag given the above inputs.
    Move "5bc94fbc3221a5db94fae95ae7121a47" To T

    // Note: The above data are the values for test vector #4 from 
    // the PDF document at: http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-spec.pdf

    // EncodingMode specifies the encoding of the output for
    // encryption, and the input for decryption.
    // It may be "hex", "url", "base64", or "quoted-printable".
    Set ComEncodingMode Of hoCrypt To "hex"

    // Set the secret key and IV
    Send ComSetEncodedIV To hoCrypt sIV "hex"
    Send ComSetEncodedKey To hoCrypt K "hex"

    // Set the additional authenticated data (AAD)
    Get ComSetEncodedAad Of hoCrypt sAAD "hex" To iSuccess

    // For the purpose of duplicating the test vectors, we are using the EncryptEncoded method.
    // This method decodes the input string according to the encoding specified by the EncodingMode
    // property, which in this case is "hex".  The decoded bytes are encrypted using the mode specified
    // by the CipherMode property.  The resulting
    // encrypted bytes are encoded (again using the encoding mode specified by EncodingMode),
    // and the result is returned.
    // <b>Note:</b> The CipherMode property sets the block mode of operation (gcm, cfb, cbc, ofb, ecb, etc.) 
    // for any of the Chilkat encryption/decryption methods (such as EncryptBytes, EncryptString, 
    // CkEncryptFile, etc.)   Just because GCM mode is demonstrated with EncryptEncoded/DecryptEncoded,
    // does not imply that GCM mode is specific to only these methods.
    Get ComEncryptEncoded Of hoCrypt sPT To sCtResult
    Get ComLastMethodSuccess Of hoCrypt To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoCrypt To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Examine the result.  It should be the same (case insensitive) as our expected result:
    Showln "computed result: " sCtResult
    Showln "expected result: " sCT

    // Examine the authenticated tag. It should be the same (case insensitive) as our expected authenticated tag:
    Get ComGetEncodedAuthTag Of hoCrypt "hex" To sTResult
    Showln "computed authTag: " sTResult
    Showln "expected authTag: " T

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

    // 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 (case insensitive) to 
    // the expected result.
    // Note: The return value of SetEncodedAuthTag indicates whether the string passed was a valid
    // representation of the encoding specified in the 2nd arg.
    Get ComSetEncodedAuthTag Of hoCrypt T "hex" To iSuccess

    // All of our properties (IV, secret key, cipher mode, and AAD) are already set from the code above...

    // So let's decrypt CT to and check to see if we get PT.
    Get ComDecryptEncoded Of hoCrypt sCT To sPtResult
    Get ComLastMethodSuccess Of hoCrypt To bTemp1
    If (bTemp1 <> True) Begin
        // Failed.  The resultant authenticated tag did not equal the expected authentication tag.
        Get ComLastErrorText Of hoCrypt To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Examine the decrypted result.  It should be the same as our expected plaintext (case insensitive)
    Showln "plaintext decrypted: " sPtResult
    Showln "plaintext expected:  " sPT

    // Let's intentionally set the expected authenticated tag to an incorrect value. 
    // The decrypt operation should fail:
    Move "ffaabbbc3221a5db94fae95ae7121a47" To sTInvalid

    Get ComSetEncodedAuthTag Of hoCrypt sTInvalid "hex" To iSuccess

    Get ComDecryptEncoded Of hoCrypt sCT To sPtResult
    Get ComLastMethodSuccess Of hoCrypt To bTemp1
    If (bTemp1 <> True) Begin
        // Failed.  The resultant authenticated tag did not equal the expected authentication tag.
        Get ComLastErrorText Of hoCrypt To sTemp1
        Showln sTemp1
    End



End_Procedure

 

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