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) Tips on Matching Encryption with another System

This example provides tips on matching encryption results produced by another system.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoCrypt
    String sIvHex1
    String sIvHex2
    String sKeyHex
    String sTemp1

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

    // Let's examine 256-bit AES encryption in CBC mode.
    // CBC mode is Cipher Block Chaining, and it uses an IV (initialization vector)
    Set ComCryptAlgorithm Of hoCrypt To "aes"
    Set ComCipherMode Of hoCrypt To "cbc"
    Set ComKeyLength Of hoCrypt To 256
    Set ComPaddingScheme Of hoCrypt To 0
    Move "000102030405060708090A0B0C0D0E0F" To sIvHex1
    Move "FF0102030405060708090A0B0C0D0E0F" To sIvHex2
    Send ComSetEncodedIV To hoCrypt sIvHex1 "hex"
    Move "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F" To sKeyHex
    Send ComSetEncodedKey To hoCrypt sKeyHex "hex"

    // Matching encryption requires all of the above settings to be matched exactly.
    // Let's get our output in hex format so we can easily see the values of the encrypted bytes.
    Set ComEncodingMode Of hoCrypt To "hex"

    // Encrypt something small:
    Get ComEncryptStringENC Of hoCrypt "Hello" To sTemp1
    Showln sTemp1
    // The result is 5B827AB3B4F9F2292C2B74C8A6C99A3D
    // This 16 bytes -- exactly one AES encryption block.

    // Let's change only the padding scheme.
    Set ComPaddingScheme Of hoCrypt To 3

    // Encrypt again:
    Get ComEncryptStringENC Of hoCrypt "Hello" To sTemp1
    Showln sTemp1
    // The result is entirely different: 469C28CC576069F807891FEE2DE76D68

    // The padding scheme only affects the very last block of output.  Therefore,
    // if all settings match except for the padding scheme, we're unable to
    // know if we encrypt a very small amount of data. However, if we encrypt
    // a larger amount of data, the single difference becomes apparent:
    Showln "-- Only the padding scheme differs --"
    Set ComPaddingScheme Of hoCrypt To 0
    Get ComEncryptStringENC Of hoCrypt "HelloHelloHelloHelloHelloHelloHello" To sTemp1
    Showln sTemp1
    Set ComPaddingScheme Of hoCrypt To 3
    Get ComEncryptStringENC Of hoCrypt "HelloHelloHelloHelloHelloHelloHello" To sTemp1
    Showln sTemp1

    // Now examine the outputs:
    // F6A201F8E0B6595FA20E4A212A2AD9A5046DAF29E8B35AD15CEE56A1A69F2A3A7B347A7C15E26E7A6760533C7A8E0D44
    // F6A201F8E0B6595FA20E4A212A2AD9A5046DAF29E8B35AD15CEE56A1A69F2A3A292CA61D03A85E1AC39B50D4DA71691E
    // We can see the output matches except for the last block, which is affected by the padding scheme.

    // If we are able to easily use ECB mode w/ the other system
    // we are trying to match, then eliminate the IV from the picture.
    // If the encryption matches in ECB mode, but not in CBC mode,
    // then we know all correct except for the IV.
    // For example, you can see how the IV changes everything with CBC mode,
    // but it's not used in ECB mode:
    Set ComPaddingScheme Of hoCrypt To 0
    Set ComCipherMode Of hoCrypt To "cbc"
    Showln "-- Only the IV differs, CBC mode produces different output. --"
    Send ComSetEncodedIV To hoCrypt sIvHex1 "hex"
    Get ComEncryptStringENC Of hoCrypt "HelloHelloHelloHelloHelloHelloHello" To sTemp1
    Showln sTemp1
    Send ComSetEncodedIV To hoCrypt sIvHex2 "hex"
    Get ComEncryptStringENC Of hoCrypt "HelloHelloHelloHelloHelloHelloHello" To sTemp1
    Showln sTemp1

    Set ComCipherMode Of hoCrypt To "ecb"
    Showln "-- Only the IV differs, ECB does not use the IV.  The outputs are the same. --"
    Send ComSetEncodedIV To hoCrypt sIvHex1 "hex"
    Get ComEncryptStringENC Of hoCrypt "HelloHelloHelloHelloHelloHelloHello" To sTemp1
    Showln sTemp1
    Send ComSetEncodedIV To hoCrypt sIvHex2 "hex"
    Get ComEncryptStringENC Of hoCrypt "HelloHelloHelloHelloHelloHelloHello" To sTemp1
    Showln sTemp1

    // If we can eliminate the padding scheme and IV from the degrees of freedom,
    // then the only remaining likely differences are (1) the secret key,
    // and (2) the input data itself.

    // The secret key is composed of binary bytes of exactly KeyLength bits.
    // For 256-bit AES encrytion, the key length is 256, and therefore the 
    // secret key is exactly 32 bytes.  (32 * 8 bits/byte = 256 bits)
    // If the secret key is derived from an arbitrary password string, then one must
    // exactly duplicate the derivation scheme (such as PBKDF2, for example)
    // The input bytes to the derivation scheme must also match.  For example,
    // is it the utf-8 byte representation of the password string that is used
    // as the starting point for the derivation, or perhaps utf-16, or ANSI (1 byte per char)?

    // Likewise, if the data being encrypted is a string, what byte representation of
    // the string is being encrypted?  If the bytes presented to the encryptor are different,
    // then the output is different.


End_Procedure

 

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