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

 

 

 

(PowerBuilder) Encrypt File in Chunks using AES CBC

Demonstrates how to use the FirstChunk/LastChunk properties to encrypt a file chunk-by-chunk.

Important This example requires Chilkat v9.5.0.68 or later.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

integer li_rc
oleobject loo_Crypt
string ls_FileToEncrypt
oleobject loo_FacIn
integer li_Success
string ls_OutputEncryptedFile
oleobject loo_FacOutEnc
integer li_ChunkSize
integer li_NumChunks
oleobject loo_Bd
integer i
string ls_DecryptedFile
integer li_BSame

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

loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat_9_5_0.Crypt2")
if li_rc < 0 then
    destroy loo_Crypt
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Crypt.CryptAlgorithm = "aes"
loo_Crypt.CipherMode = "cbc"
loo_Crypt.KeyLength = 128

loo_Crypt.SetEncodedKey("000102030405060708090A0B0C0D0E0F","hex")
loo_Crypt.SetEncodedIV("000102030405060708090A0B0C0D0E0F","hex")

ls_FileToEncrypt = "qa_data/hamlet.xml"
loo_FacIn = create oleobject
li_rc = loo_FacIn.ConnectToNewObject("Chilkat_9_5_0.FileAccess")

li_Success = loo_FacIn.OpenForRead(ls_FileToEncrypt)
if li_Success <> 1 then
    Write-Debug "Failed to open file that is to be encrytped."
    destroy loo_Crypt
    destroy loo_FacIn
    return
end if

ls_OutputEncryptedFile = "qa_output/hamlet.enc"
loo_FacOutEnc = create oleobject
li_rc = loo_FacOutEnc.ConnectToNewObject("Chilkat_9_5_0.FileAccess")

li_Success = loo_FacOutEnc.OpenForWrite(ls_OutputEncryptedFile)
if li_Success <> 1 then
    Write-Debug "Failed to encrypted output file."
    destroy loo_Crypt
    destroy loo_FacIn
    destroy loo_FacOutEnc
    return
end if

// Let's encrypt in 10000 byte chunks.
li_ChunkSize = 10000
li_NumChunks = loo_FacIn.GetNumBlocks(li_ChunkSize)

loo_Crypt.FirstChunk = 1
loo_Crypt.LastChunk = 0

loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat_9_5_0.BinData")

i = 0
do while i < li_NumChunks
    i = i + 1
    if i = li_NumChunks then
        loo_Crypt.LastChunk = 1
    end if

    // Read the next chunk from the file.
    // The last chunk will be whatever amount remains in the file..
    loo_Bd.Clear()
    loo_FacIn.FileReadBd(li_ChunkSize,loo_Bd)

    // Encrypt.
    loo_Crypt.EncryptBd(loo_Bd)

    // Write the encrypted chunk to the output file.
    loo_FacOutEnc.FileWriteBd(loo_Bd,0,0)

    loo_Crypt.FirstChunk = 0
loop

// Make sure both FirstChunk and LastChunk are restored to 1 after
// encrypting or decrypting in chunks.  Otherwise subsequent encryptions/decryptions
// will produce unexpected results.
loo_Crypt.FirstChunk = 1
loo_Crypt.LastChunk = 1

loo_FacIn.FileClose()
loo_FacOutEnc.FileClose()

// Decrypt the encrypted output file in a single call using CBC mode:
ls_DecryptedFile = "qa_output/hamlet_dec.xml"
li_Success = loo_Crypt.CkDecryptFile(ls_OutputEncryptedFile,ls_DecryptedFile)
// Assume success for the example..

// Compare the contents of the decrypted file with the original file:
li_BSame = loo_FacIn.FileContentsEqual(ls_FileToEncrypt,ls_DecryptedFile)
Write-Debug "bSame = " + string(li_BSame)


destroy loo_Crypt
destroy loo_FacIn
destroy loo_FacOutEnc
destroy loo_Bd

 

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