Sample code for 30+ languages & platforms
PureBasic

Streaming Encryption by Encrypting in Chunks

See more Encryption Examples

Encrypt data in chunks.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkCrypt2.pb"
IncludeFile "CkStringBuilder.pb"

Procedure ChilkatExample()

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

    crypt.i = CkCrypt2::ckCreate()
    If crypt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkCrypt2::setCkCryptAlgorithm(crypt, "aes")
    CkCrypt2::setCkCipherMode(crypt, "cbc")
    CkCrypt2::setCkKeyLength(crypt, 128)

    CkCrypt2::ckSetEncodedKey(crypt,"000102030405060708090A0B0C0D0E0F","hex")
    CkCrypt2::ckSetEncodedIV(crypt,"000102030405060708090A0B0C0D0E0F","hex")

    CkCrypt2::setCkEncodingMode(crypt, "hex")
    txt1.s = "The quick brown fox jumped over the lazy dog." + Chr(13) + Chr(10)
    txt2.s = "-" + Chr(13) + Chr(10)
    txt3.s = "Done." + Chr(13) + Chr(10)

    sbEncrypted.i = CkStringBuilder::ckCreate()
    If sbEncrypted.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; 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.)
    CkCrypt2::setCkFirstChunk(crypt, 1)
    CkCrypt2::setCkLastChunk(crypt, 0)
    CkStringBuilder::ckAppend(sbEncrypted,CkCrypt2::ckEncryptStringENC(crypt,txt1))

    ; Encrypt the 2nd chunk
    CkCrypt2::setCkFirstChunk(crypt, 0)
    CkCrypt2::setCkLastChunk(crypt, 0)
    CkStringBuilder::ckAppend(sbEncrypted,CkCrypt2::ckEncryptStringENC(crypt,txt2))

    ; 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...
    CkCrypt2::setCkFirstChunk(crypt, 0)
    CkCrypt2::setCkLastChunk(crypt, 0)
    i.i
    For i = 0 To 4
        CkStringBuilder::ckAppend(sbEncrypted,CkCrypt2::ckEncryptStringENC(crypt,txt1))
        CkStringBuilder::ckAppend(sbEncrypted,CkCrypt2::ckEncryptStringENC(crypt,txt2))
    Next

    ; Now encrypt the last chunk:
    CkCrypt2::setCkFirstChunk(crypt, 0)
    CkCrypt2::setCkLastChunk(crypt, 1)
    CkStringBuilder::ckAppend(sbEncrypted,CkCrypt2::ckEncryptStringENC(crypt,txt3))

    Debug CkStringBuilder::ckGetAsString(sbEncrypted)

    ; Now decrypt in one call.
    ; (The data we're decrypting is both the first AND last chunk.)  
    CkCrypt2::setCkFirstChunk(crypt, 1)
    CkCrypt2::setCkLastChunk(crypt, 1)
    decryptedText.s = CkCrypt2::ckDecryptStringENC(crypt,CkStringBuilder::ckGetAsString(sbEncrypted))

    Debug decryptedText

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


    CkCrypt2::ckDispose(crypt)
    CkStringBuilder::ckDispose(sbEncrypted)


    ProcedureReturn
EndProcedure