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 Encryption

Encrypt and decrypt using a stream.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

integer li_rc
oleobject loo_Crypt
string ls_IvHex
string ls_KeyHex
string ls_PlainText
oleobject loo_Stream
oleobject loo_EStrings
oleobject loo_Task
integer li_Success
string ls_CipherText
integer i
integer n

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample 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

// Setup encryption using the chacha20 algorithm...
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")

ls_PlainText = "The quick brown fox jumped over the lazy dog.~r~n"

loo_Stream = create oleobject
li_rc = loo_Stream.ConnectToNewObject("Chilkat_9_5_0.Stream")

// We'll save the encrypted output in eStrings to demonstrate streaming decryption next.
loo_EStrings = create oleobject
li_rc = loo_EStrings.ConnectToNewObject("Chilkat_9_5_0.StringArray")

// Start a background task that will encrypt a stream.
loo_Task = loo_Crypt.EncryptStreamAsync(loo_Stream)
li_Success = loo_Task.Run()

// Write plainText to the stream, and read chacha20 encrypted text..

for i = 1 to 10
    // Note: An encryption algorithm's block size will cause buffering,
    // and therefore not every loop iteration will produce output.
    li_Success = loo_Stream.WriteString(ls_PlainText)
    if loo_Stream.DataAvailable = 1 then
        ls_CipherText = loo_Stream.ReadBytesENC("hex")
        Write-Debug ls_CipherText
        li_Success = loo_EStrings.Append(ls_CipherText)
    end if

next

// Tell the background task that the stream has ended.
li_Success = loo_Stream.WriteClose()

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

// Get any remaining data available from the stream.
if loo_Stream.DataAvailable = 1 then
    ls_CipherText = loo_Stream.ReadBytesENC("hex")
    Write-Debug ls_CipherText
    li_Success = loo_EStrings.Append(ls_CipherText)
end if

if loo_Task.TaskSuccess <> 1 then
    Write-Debug "async encryption failed:"
    Write-Debug loo_Task.ResultErrorText
    li_Success = 0
end if

destroy loo_Task

Write-Debug "-- encrypt finished --"

// Now decrypt to return the original.

// Reset the stream object so it can be used again.
loo_Stream.Reset()

// Start a background task that will decrypt a stream.
loo_Task = loo_Crypt.DecryptStreamAsync(loo_Stream)
li_Success = loo_Task.Run()

n = loo_EStrings.Count
for i = 0 to n - 1
    li_Success = loo_Stream.WriteBytesENC(loo_EStrings.GetString(i),"hex")
    if loo_Stream.DataAvailable = 1 then
        ls_PlainText = loo_Stream.ReadString()
        Write-Debug ls_PlainText
    end if

next

// Tell the background task that the stream has ended.
li_Success = loo_Stream.WriteClose()

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

// Get any remaining data available from the stream.
if loo_Stream.DataAvailable = 1 then
    ls_PlainText = loo_Stream.ReadString()
    Write-Debug ls_PlainText
end if

if loo_Task.TaskSuccess <> 1 then
    Write-Debug "async decryption failed:"
    Write-Debug loo_Task.ResultErrorText
    li_Success = 0
end if

destroy loo_Task

Write-Debug "-- decrypt finished --"


destroy loo_Crypt
destroy loo_Stream
destroy loo_EStrings

 

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