C
C
Workaround for the deprecated Crypt2.DecryptBytesENC method
Shows how to replace the deprecated DecryptBytesENC method. (Chilkat is moving away from the use of CkByteData.)Chilkat C Downloads
#include <C_CkCrypt2.h>
#include <C_CkByteData.h>
#include <C_CkBinData.h>
void ChilkatSample(void)
{
BOOL success;
HCkCrypt2 crypt;
HCkByteData unencryptedBytes;
HCkByteData decryptedBytes;
const char *encryptedAsHex;
HCkBinData bd;
HCkBinData bd2;
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");
success = FALSE;
// ------------------------------------------------------------------------
// The EncryptBytesENC and DecryptBytesENC methods are deprecated:
unencryptedBytes = CkByteData_Create();
CkByteData_appendEncoded(unencryptedBytes,"AABBCCDDEEFF01020304","hex");
decryptedBytes = CkByteData_Create();
CkCrypt2_putEncodingMode(crypt,"hex");
encryptedAsHex = CkCrypt2_encryptBytesENC(crypt,unencryptedBytes);
printf("Encrypted: %s\n",encryptedAsHex);
success = CkCrypt2_DecryptBytesENC(crypt,encryptedAsHex,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, then get as hex.
CkCrypt2_EncryptBd(crypt,bd);
encryptedAsHex = CkBinData_getEncoded(bd,"hex");
printf("Encrypted: %s\n",encryptedAsHex);
// load from hex, then decrypt.
bd2 = CkBinData_Create();
CkBinData_AppendEncoded(bd2,encryptedAsHex,"hex");
CkCrypt2_DecryptBd(crypt,bd2);
printf("Decrypted: %s\n",CkBinData_getEncoded(bd2,"hex"));
CkCrypt2_Dispose(crypt);
CkByteData_Dispose(unencryptedBytes);
CkByteData_Dispose(decryptedBytes);
CkBinData_Dispose(bd);
CkBinData_Dispose(bd2);
}