Sample code for 30+ languages & platforms
PureBasic

AES and CHACHA20 Encrypt/Decrypt Text

See more Encryption Examples

Demonstrates the use of the new EncryptSb and DecryptSb methods introduced in Chilkat v9.5.0.67 to encrypt/decrypt the contents of StringBuilder objects.

Note: This example requires Chilkat v9.5.0.67 or greater.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkStringBuilder.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.

    ; First build a string to be encrypted
    sb.i = CkStringBuilder::ckCreate()
    If sb.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    i.i = 1
    While i < 10
        CkStringBuilder::ckAppendInt(sb,i)
        CkStringBuilder::ckAppend(sb," the quick brown fox jumped over the lazy dog." + Chr(13) + Chr(10))
        i = i + 1
    Wend

    Debug CkStringBuilder::ckGetAsString(sb)

    ; The string to be encrypted looks like this:

    ; 1 the quick brown fox jumped over the lazy dog.
    ; 2 the quick brown fox jumped over the lazy dog.
    ; 3 the quick brown fox jumped over the lazy dog.
    ; 4 the quick brown fox jumped over the lazy dog.
    ; 5 the quick brown fox jumped over the lazy dog.
    ; 6 the quick brown fox jumped over the lazy dog.
    ; ...

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

    ; Specify the encryption to be used.
    ; First we'll do AES-128 CBC
    CkCrypt2::setCkCryptAlgorithm(crypt, "aes")
    CkCrypt2::setCkCipherMode(crypt, "cbc")
    CkCrypt2::setCkKeyLength(crypt, 128)

    ivHex.s = "000102030405060708090A0B0C0D0E0F"
    CkCrypt2::ckSetEncodedIV(crypt,ivHex,"hex")

    keyHex.s = "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"
    CkCrypt2::ckSetEncodedKey(crypt,keyHex,"hex")

    ; When EncryptSb is called, the contents of the source (sb)
    ; remains unmodified.  The encrypted bytes are written into bdEncrypted.
    ; If bdEncrypted already contained data, it is replaced with the encrypted bytes.
    bdEncrypted.i = CkBinData::ckCreate()
    If bdEncrypted.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCrypt2::ckEncryptSb(crypt,sb,bdEncrypted)
    If success <> 1
        Debug CkCrypt2::ckLastErrorText(crypt)
        CkStringBuilder::ckDispose(sb)
        CkCrypt2::ckDispose(crypt)
        CkBinData::ckDispose(bdEncrypted)
        ProcedureReturn
    EndIf

    ; Examine the encrypted bytes:
    Debug CkBinData::ckGetEncoded(bdEncrypted,"base64_mime")

    ; Sample encrypted data:

    ; 0DdNZ22ckMMJBiaKhAu3wUEfh16XW356NIUsDmNs/xwbHe/p1201OmpfzwXwKktkAefu2pckrBgC
    ; df+1w8lRo+KAy5n5wlAgMGM/UrsVJsp0BmDPk1vaxKrmrGpSXOVCQs1n2+0atIs5YLiOG+Va3+Mi
    ; EQNb4YK7bNMmvt0++irBxTiGnkx/RncfKwgkbBUpl2x7yV13MW6lapDT6Md0DKAMsTXFJYGeIdEf
    ; g2uxDDQzI5gUOUHTMrXQ8paD/K76KKB9Jpp/kAM9z8g/d8KUmuphA7KI64d38xsgOmcITlbhlCQ2
    ; PDkcU6RRzX0FUTUSMgQukhy0jkLZEjHX9poKJD+iJTOkcQUC3OqR9hKhSrvIgJN4lxdR71MheOoQ
    ; 2wmvRdq+agTWWh333Vmb6J6yDV79aSpnqEDrA8Ks7Xzciol0gve91+JtVJlJKjWwEzWEU8GxF7Q8
    ; eaWI70lsC5nTLGcbqgKu6gzkzHlHyHaE2FAQA/d5I2dvfsAYUQCza0Zdyw8mmTtHhlP2Tfxj1uPv
    ; H4Q7BGuKnx3SWT2CnpbX4091w7KzLAztrbFBo/Tf9w8ZpgTK9k1ryfW9/xnk6rW6iQ==

    ; Decrypt to restore back to the original:
    sbOriginal.i = CkStringBuilder::ckCreate()
    If sbOriginal.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCrypt2::ckDecryptSb(crypt,bdEncrypted,sbOriginal)
    If success <> 1
        Debug CkCrypt2::ckLastErrorText(crypt)
        CkStringBuilder::ckDispose(sb)
        CkCrypt2::ckDispose(crypt)
        CkBinData::ckDispose(bdEncrypted)
        CkStringBuilder::ckDispose(sbOriginal)
        ProcedureReturn
    EndIf

    ; Examine the original data:
    Debug CkStringBuilder::ckGetAsString(sbOriginal)

    ; ----------------------------------------------------------------------------------
    ; To do chacha20 encryption, just change the settings:

    CkCrypt2::setCkCryptAlgorithm(crypt, "chacha20")
    CkCrypt2::setCkKeyLength(crypt, 256)
    ; The initial count is the initial block counter for the chacha20 algorithm.
    ; It can be any integer, but must be set to the same when decrypting.
    CkCrypt2::setCkInitialCount(crypt, 22)

    ; EncryptSb completely replaces the contents of bdEncrypted.
    success = CkCrypt2::ckEncryptSb(crypt,sb,bdEncrypted)

    ; However.. DecryptSb appends the decrypted text to whatever may
    ; have already existed within sbOriginal.
    success = CkCrypt2::ckDecryptSb(crypt,bdEncrypted,sbOriginal)

    Debug "----"
    Debug "The original data should be here twice.  Once from the AES decrypt, and again from the chacha20 decrypt."
    Debug CkStringBuilder::ckGetAsString(sbOriginal)
    Debug "Success."


    CkStringBuilder::ckDispose(sb)
    CkCrypt2::ckDispose(crypt)
    CkBinData::ckDispose(bdEncrypted)
    CkStringBuilder::ckDispose(sbOriginal)


    ProcedureReturn
EndProcedure