Sample code for 30+ languages & platforms
C

Workaround for the deprecated EncryptBytes and DecryptBytes methods

Shows how to replace the deprecated DecryptBytes method. (Chilkat is moving away from the use of CkByteData.)

Chilkat C Downloads

C
#include <C_CkCrypt2.h>
#include <C_CkByteData.h>
#include <C_CkBinData.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkCrypt2 crypt;
    HCkByteData unencryptedBytes;
    HCkByteData encryptedBytes;
    HCkByteData decryptedBytes;
    HCkBinData bd;

    success = FALSE;

    crypt = CkCrypt2_Create();

    CkCrypt2_putCryptAlgorithm(crypt,"aes");
    CkCrypt2_putCipherMode(crypt,"cbc");
    CkCrypt2_putKeyLength(crypt,128);
    CkCrypt2_putPaddingScheme(crypt,0);
    CkCrypt2_SetEncodedKey(crypt,"000102030405060708090A0B0C0D0E0F","hex");
    CkCrypt2_SetEncodedIV(crypt,"000102030405060708090A0B0C0D0E0F","hex");
    CkCrypt2_putEncodingMode(crypt,"base64");

    success = FALSE;

    //  ------------------------------------------------------------------------
    //  The EncryptBytes and DecryptBytes methods are deprecated and will eventually be removed.

    unencryptedBytes = CkByteData_Create();
    CkByteData_appendEncoded(unencryptedBytes,"AABBCCDDEEFF01020304","hex");

    encryptedBytes = CkByteData_Create();
    decryptedBytes = CkByteData_Create();

    success = CkCrypt2_EncryptBytes(crypt,unencryptedBytes,encryptedBytes);
    success = CkCrypt2_DecryptBytes(crypt,encryptedBytes,decryptedBytes);

    //  ------------------------------------------------------------------------
    //  Replace the above CkByteData usage with the following code:
    //  (Chilkat is moving away from using CkByteData)

    bd = CkBinData_Create();
    CkBinData_AppendEncoded(bd,"AABBCCDDEEFF01020304","hex");

    //  in-place encrypt
    CkCrypt2_EncryptBd(crypt,bd);
    printf("Encrypted: %s\n",CkBinData_getEncoded(bd,"hex"));

    //  in-place decrypt
    CkCrypt2_DecryptBd(crypt,bd);
    printf("Decrypted: %s\n",CkBinData_getEncoded(bd,"hex"));


    CkCrypt2_Dispose(crypt);
    CkByteData_Dispose(unencryptedBytes);
    CkByteData_Dispose(encryptedBytes);
    CkByteData_Dispose(decryptedBytes);
    CkBinData_Dispose(bd);

    }