Sample code for 30+ languages & platforms
Xbase++

Streaming Encryption by Encrypting in Chunks

See more Encryption Examples

Encrypt data in chunks.

Chilkat Xbase++ Downloads

Xbase++
LOCAL oCrypt
LOCAL cTxt1
LOCAL cTxt2
LOCAL cTxt3
LOCAL oSbEncrypted
LOCAL i
LOCAL cDecryptedText

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

oCrypt := CreateObject("Chilkat.Crypt2")

oCrypt:CryptAlgorithm := "aes"
oCrypt:CipherMode := "cbc"
oCrypt:KeyLength := 128

oCrypt:SetEncodedKey("000102030405060708090A0B0C0D0E0F", "hex")
oCrypt:SetEncodedIV("000102030405060708090A0B0C0D0E0F", "hex")

oCrypt:EncodingMode := "hex"
cTxt1 := "The quick brown fox jumped over the lazy dog." + Chr(13) + Chr(10)
cTxt2 := "-" + Chr(13) + Chr(10)
cTxt3 := "Done." + Chr(13) + Chr(10)

oSbEncrypted := CreateObject("Chilkat.StringBuilder")

//  Encrypt the 1st chunk:
//  (don't worry about feeding the data to the encryptor in 
//  exact multiples of the encryption algorithm's block size.
//  Chilkat will buffer the data.)
oCrypt:FirstChunk := 1
oCrypt:LastChunk := 0
oSbEncrypted:Append(oCrypt:EncryptStringENC(cTxt1))

//  Encrypt the 2nd chunk
oCrypt:FirstChunk := 0
oCrypt:LastChunk := 0
oSbEncrypted:Append(oCrypt:EncryptStringENC(cTxt2))

//  Now encrypt N more chunks...
//  Remember -- we're doing this in CBC mode, so each call
//  to the encrypt method depends on the state from previous
//  calls...
oCrypt:FirstChunk := 0
oCrypt:LastChunk := 0

FOR i := 0 TO 4
    oSbEncrypted:Append(oCrypt:EncryptStringENC(cTxt1))
    oSbEncrypted:Append(oCrypt:EncryptStringENC(cTxt2))
NEXT

//  Now encrypt the last chunk:
oCrypt:FirstChunk := 0
oCrypt:LastChunk := 1
oSbEncrypted:Append(oCrypt:EncryptStringENC(cTxt3))

? oSbEncrypted:GetAsString()

//  Now decrypt in one call.
//  (The data we're decrypting is both the first AND last chunk.)  
oCrypt:FirstChunk := 1
oCrypt:LastChunk := 1
cDecryptedText := oCrypt:DecryptStringENC(oSbEncrypted:GetAsString())

? cDecryptedText

//  Note: You may decrypt in N chunks by setting the FirstChunk
//  and LastChunk properties prior to calling the Decrypt* methods.

oCrypt:destroy()
oSbEncrypted:destroy()