Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

PowerBuilder Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
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
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(PowerBuilder) Streaming Compression --> Streaming Encryption

Demonstrate how to feed a compression stream into an encryption stream. This example does the following:

Data File --> Compress --> Encrypt --> Application Reads

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

integer li_rc
integer li_Success
oleobject loo_Compress
oleobject loo_Crypt
string ls_IvHex
string ls_KeyHex
oleobject loo_StreamC
oleobject loo_StreamE
oleobject loo_TaskC
oleobject loo_TaskE
oleobject loo_OutData

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

//  Initialize the compression object.
loo_Compress = create oleobject
li_rc = loo_Compress.ConnectToNewObject("Chilkat_9_5_0.Compression")
if li_rc < 0 then
    destroy loo_Compress
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Compress.Algorithm = "deflate"

//  Initialize the encryption object.
loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat_9_5_0.Crypt2")

loo_Crypt.CryptAlgorithm = "chacha20"
loo_Crypt.KeyLength = 256
loo_Crypt.EncodingMode = "hex"
ls_IvHex = "000000000000000000000002"
loo_Crypt.SetEncodedIV(ls_IvHex,"hex")
loo_Crypt.InitialCount = 42
ls_KeyHex = "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0"
loo_Crypt.SetEncodedKey(ls_KeyHex,"hex")

//  Setup the streams.
//  streamC is for compression
loo_StreamC = create oleobject
li_rc = loo_StreamC.ConnectToNewObject("Chilkat_9_5_0.Stream")

//  streamE is for encryption
loo_StreamE = create oleobject
li_rc = loo_StreamE.ConnectToNewObject("Chilkat_9_5_0.Stream")

//  The source for the compressor is a file.
loo_StreamC.SourceFile = "qa_data/hamlet.xml"
//  The source for the encryptor is the output of the compressor stream.
li_Success = loo_StreamE.SetSourceStream(loo_StreamC)

//  Our application will be reading the output of streamE.
//  Start the compression and encryption streams in background threads..
loo_TaskC = loo_Compress.CompressStreamAsync(loo_StreamC)
loo_TaskE = loo_Crypt.EncryptStreamAsync(loo_StreamE)
li_Success = loo_TaskC.Run()
li_Success = loo_TaskE.Run()

//  Read the compressed and encrypted bytes.
loo_OutData = create oleobject
li_rc = loo_OutData.ConnectToNewObject("Chilkat_9_5_0.BinData")

do while (loo_StreamE.EndOfStream <> 1)
    if loo_StreamE.DataAvailable = 1 then
        li_Success = loo_StreamE.ReadBd(loo_OutData)
    end if

loop

//  Let's make sure the background task finished.
//  It should already be the case that the task is finished.
do while ((loo_TaskE.Finished <> 1) OR (loo_TaskC.Finished <> 1))
    loo_TaskE.SleepMs(20)
loop

//  Get any remaining data that needs to be flushed.
if loo_StreamE.DataAvailable = 1 then
    li_Success = loo_StreamE.ReadBd(loo_OutData)
end if

//  Double-check for success..
if loo_TaskE.TaskSuccess <> 1 then
    Write-Debug "async encryption failed:"
    Write-Debug loo_TaskE.ResultErrorText
    li_Success = 0
end if

//  Double-check for success..
if loo_TaskC.TaskSuccess <> 1 then
    Write-Debug "async compression failed:"
    Write-Debug loo_TaskC.ResultErrorText
    li_Success = 0
end if

destroy loo_TaskE
destroy loo_TaskC

//  Save the compressed/encrypted data to a file.
loo_OutData.WriteFile("qa_output/compressedEncrypted.dat")

Write-Debug "The async compress/encrypt was successful."

//  Let's decrypt and decompress to further verify...
li_Success = loo_Crypt.DecryptBd(loo_OutData)
li_Success = loo_Compress.DecompressBd(loo_OutData)

//  outData should contain the original data..
li_Success = loo_OutData.WriteFile("qa_output/original_hamlet.xml")


destroy loo_Compress
destroy loo_Crypt
destroy loo_StreamC
destroy loo_StreamE
destroy loo_OutData

 

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