Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkFileAccess.pb"
IncludeFile "CkCrypt2.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example assumes 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, 256)

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

    fileToEncrypt.s = "qa_data/hamlet.xml"
    facIn.i = CkFileAccess::ckCreate()
    If facIn.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkFileAccess::ckOpenForRead(facIn,fileToEncrypt)
    If success <> 1
        Debug "Failed to open file that is to be encrytped."
        CkCrypt2::ckDispose(crypt)
        CkFileAccess::ckDispose(facIn)
        ProcedureReturn
    EndIf

    outputEncryptedFile.s = "c:/temp/qa_output/hamlet.enc"
    facOutEnc.i = CkFileAccess::ckCreate()
    If facOutEnc.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkFileAccess::ckOpenForWrite(facOutEnc,outputEncryptedFile)
    If success <> 1
        Debug "Failed to encrypted output file."
        CkCrypt2::ckDispose(crypt)
        CkFileAccess::ckDispose(facIn)
        CkFileAccess::ckDispose(facOutEnc)
        ProcedureReturn
    EndIf

    ; Let's encrypt in 10000 byte chunks.
    chunkSize.i = 10000
    numChunks.i = CkFileAccess::ckGetNumBlocks(facIn,chunkSize)

    CkCrypt2::setCkFirstChunk(crypt, 1)
    CkCrypt2::setCkLastChunk(crypt, 0)

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

    i.i = 0
    While i < numChunks
        i = i + 1
        If i = numChunks
            CkCrypt2::setCkLastChunk(crypt, 1)
        EndIf

        ; Read the next chunk from the file.
        ; The last chunk will be whatever amount remains in the file..
        CkBinData::ckClear(bd)
        CkFileAccess::ckFileReadBd(facIn,chunkSize,bd)

        ; Encrypt.
        CkCrypt2::ckEncryptBd(crypt,bd)

        ; Write the encrypted chunk to the output file.
        CkFileAccess::ckFileWriteBd(facOutEnc,bd,0,0)

        CkCrypt2::setCkFirstChunk(crypt, 0)
    Wend

    ; Make sure both FirstChunk and LastChunk are restored to 1 after
    ; encrypting or decrypting in chunks.  Otherwise subsequent encryptions/decryptions
    ; will produce unexpected results.
    CkCrypt2::setCkFirstChunk(crypt, 1)
    CkCrypt2::setCkLastChunk(crypt, 1)

    CkFileAccess::ckFileClose(facIn)
    CkFileAccess::ckFileClose(facOutEnc)

    ; Decrypt the encrypted output file in a single call using CBC mode:
    decryptedFile.s = "qa_output/hamlet_dec.xml"
    success = CkCrypt2::ckCkDecryptFile(crypt,outputEncryptedFile,decryptedFile)
    ; Assume success for the example..

    ; Compare the contents of the decrypted file with the original file:
    bSame.i = CkFileAccess::ckFileContentsEqual(facIn,fileToEncrypt,decryptedFile)
    Debug "bSame = " + Str(bSame)


    CkCrypt2::ckDispose(crypt)
    CkFileAccess::ckDispose(facIn)
    CkFileAccess::ckDispose(facOutEnc)
    CkBinData::ckDispose(bd)


    ProcedureReturn
EndProcedure