Sample code for 30+ languages & platforms
PowerBuilder

Encrypt File in Chunks using AES CBC

See more Encryption Examples

Demonstrates how to use the FirstChunk/LastChunk properties to encrypt a file chunk-by-chunk.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Crypt
string ls_FileToEncrypt
oleobject loo_FacIn
string ls_OutputEncryptedFile
oleobject loo_FacOutEnc
integer li_ChunkSize
integer li_NumChunks
oleobject loo_Bd
integer i
string ls_DecryptedFile
integer li_BSame

li_Success = 0

// 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.Crypt2")
if li_rc < 0 then
    destroy loo_Crypt
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Crypt.CryptAlgorithm = "aes"
loo_Crypt.CipherMode = "cbc"
loo_Crypt.KeyLength = 256

loo_Crypt.SetEncodedKey("000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F","hex")
loo_Crypt.SetEncodedIV("000102030405060708090A0B0C0D0E0F","hex")

ls_FileToEncrypt = "qa_data/hamlet.xml"
loo_FacIn = create oleobject
li_rc = loo_FacIn.ConnectToNewObject("Chilkat.FileAccess")

li_Success = loo_FacIn.OpenForRead(ls_FileToEncrypt)
if li_Success <> 1 then
    Write-Debug "Failed to open file that is to be encrytped."
    destroy loo_Crypt
    destroy loo_FacIn
    return
end if

ls_OutputEncryptedFile = "c:/temp/qa_output/hamlet.enc"
loo_FacOutEnc = create oleobject
li_rc = loo_FacOutEnc.ConnectToNewObject("Chilkat.FileAccess")

li_Success = loo_FacOutEnc.OpenForWrite(ls_OutputEncryptedFile)
if li_Success <> 1 then
    Write-Debug "Failed to encrypted output file."
    destroy loo_Crypt
    destroy loo_FacIn
    destroy loo_FacOutEnc
    return
end if

// Let's encrypt in 10000 byte chunks.
li_ChunkSize = 10000
li_NumChunks = loo_FacIn.GetNumBlocks(li_ChunkSize)

loo_Crypt.FirstChunk = 1
loo_Crypt.LastChunk = 0

loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

i = 0
do while i < li_NumChunks
    i = i + 1
    if i = li_NumChunks then
        loo_Crypt.LastChunk = 1
    end if

    // Read the next chunk from the file.
    // The last chunk will be whatever amount remains in the file..
    loo_Bd.Clear()
    loo_FacIn.FileReadBd(li_ChunkSize,loo_Bd)

    // Encrypt.
    loo_Crypt.EncryptBd(loo_Bd)

    // Write the encrypted chunk to the output file.
    loo_FacOutEnc.FileWriteBd(loo_Bd,0,0)

    loo_Crypt.FirstChunk = 0
loop

// Make sure both FirstChunk and LastChunk are restored to 1 after
// encrypting or decrypting in chunks.  Otherwise subsequent encryptions/decryptions
// will produce unexpected results.
loo_Crypt.FirstChunk = 1
loo_Crypt.LastChunk = 1

loo_FacIn.FileClose()
loo_FacOutEnc.FileClose()

// Decrypt the encrypted output file in a single call using CBC mode:
ls_DecryptedFile = "qa_output/hamlet_dec.xml"
li_Success = loo_Crypt.CkDecryptFile(ls_OutputEncryptedFile,ls_DecryptedFile)
// Assume success for the example..

// Compare the contents of the decrypted file with the original file:
li_BSame = loo_FacIn.FileContentsEqual(ls_FileToEncrypt,ls_DecryptedFile)
Write-Debug "bSame = " + string(li_BSame)


destroy loo_Crypt
destroy loo_FacIn
destroy loo_FacOutEnc
destroy loo_Bd