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

Tcl 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

 

 

 

(Tcl) Streaming Encryption

Encrypt and decrypt using a stream.

Chilkat Tcl Extension Downloads

Chilkat Tcl Extension Downloads

load ./chilkat.dll

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

set crypt [new_CkCrypt2]

# Setup encryption using the chacha20 algorithm...
CkCrypt2_put_CryptAlgorithm $crypt "chacha20"
CkCrypt2_put_KeyLength $crypt 256
CkCrypt2_put_EncodingMode $crypt "hex"
set ivHex "000000000000000000000002"
CkCrypt2_SetEncodedIV $crypt $ivHex "hex"
CkCrypt2_put_InitialCount $crypt 42
set keyHex "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0"
CkCrypt2_SetEncodedKey $crypt $keyHex "hex"

set plainText "The quick brown fox jumped over the lazy dog.\r\n"

set stream [new_CkStream]

# We'll save the encrypted output in eStrings to demonstrate streaming decryption next.
set eStrings [new_CkStringArray]

# Start a background task that will encrypt a stream.
# task is a CkTask
set task [CkCrypt2_EncryptStreamAsync $crypt $stream]
set success [CkTask_Run $task]

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

for {set i 1} {$i <= 10} {incr i} {
    # Note: An encryption algorithm's block size will cause buffering,
    # and therefore not every loop iteration will produce output.
    set success [CkStream_WriteString $stream $plainText]
    if {[expr [CkStream_get_DataAvailable $stream] == 1]} then {
        set cipherText [CkStream_readBytesENC $stream "hex"]
        puts "$cipherText"
        set success [CkStringArray_Append $eStrings $cipherText]
    }

}

# Tell the background task that the stream has ended.
set success [CkStream_WriteClose $stream]

# Let's make sure the background task finished.
# It should already be the case that the task is finished.
while {[expr [CkTask_get_Finished $task] != 1]} {
    [CkTask_SleepMs $task 20]
}

# Get any remaining data available from the stream.
if {[expr [CkStream_get_DataAvailable $stream] == 1]} then {
    set cipherText [CkStream_readBytesENC $stream "hex"]
    puts "$cipherText"
    set success [CkStringArray_Append $eStrings $cipherText]
}

if {[expr [CkTask_get_TaskSuccess $task] != 1]} then {
    puts "async encryption failed:"
    puts [CkTask_resultErrorText $task]
    set success 0
}

delete_CkTask $task

puts "-- encrypt finished --"

# Now decrypt to return the original.

# Reset the stream object so it can be used again.
CkStream_Reset $stream

# Start a background task that will decrypt a stream.
set task [CkCrypt2_DecryptStreamAsync $crypt $stream]
set success [CkTask_Run $task]

set n [CkStringArray_get_Count $eStrings]
for {set i 0} {$i <= [expr $n - 1]} {incr i} {
    set success [CkStream_WriteBytesENC $stream [CkStringArray_getString $eStrings $i] "hex"]
    if {[expr [CkStream_get_DataAvailable $stream] == 1]} then {
        set plainText [CkStream_readString $stream]
        puts "$plainText"
    }

}

# Tell the background task that the stream has ended.
set success [CkStream_WriteClose $stream]

# Let's make sure the background task finished.
# It should already be the case that the task is finished.
while {[expr [CkTask_get_Finished $task] != 1]} {
    [CkTask_SleepMs $task 20]
}

# Get any remaining data available from the stream.
if {[expr [CkStream_get_DataAvailable $stream] == 1]} then {
    set plainText [CkStream_readString $stream]
    puts "$plainText"
}

if {[expr [CkTask_get_TaskSuccess $task] != 1]} then {
    puts "async decryption failed:"
    puts [CkTask_resultErrorText $task]
    set success 0
}

delete_CkTask $task

puts "-- decrypt finished --"

delete_CkCrypt2 $crypt
delete_CkStream $stream
delete_CkStringArray $eStrings

 

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