Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
BZIP2 compress and AES Encrypt a byte array.
This sample program demonstrates how to BZIP2 compress and then encrypt data. // The Chilkat Encryption library is also available as an ActiveX component
// or .NET class with the identical set of methods and properties.
// BZIP2 compress and AES Encrypt a byte array.
//
void CryptExample(void)
{
CkCrypt2 crypt;
// Any string passed to UnlockComponent automatically begins the 30-day trial.
crypt.UnlockComponent("30-day trial");
// Use BZIP2 compression.
crypt.put_CompressionAlgorithm("bzip2");
// Use AES encryption.
crypt.put_CryptAlgorithm("aes");
// Use 128-bit AES encryption.
// It is also possible to use 192-bit or 256-bit AES encryption.
crypt.put_KeyLength(128);
// Create a binary secret key from a password string.
CkByteData secretKey;
crypt.GenerateSecretKey("myPassword",secretKey);
crypt.put_SecretKey(secretKey);
// Note: we could just as well set the binary secret key directly.
// We need a binary key of 16 bytes (16 bytes * 8 bits/byte = 128 bits)
// Example:
// int i;
// unsigned char sKey[16];
// for (i=0; i<16; i++) sKey[i] = i;
// secretKey.append(sKey,16);
// Get some data to encrypt.
CkByteData inData;
// This XML file can be downloaded from:
// http://www.chilkatsoft.com/hamlet.xml
inData.loadFile("hamlet.xml");
CkByteData compressedData;
crypt.CompressBytes(inData,compressedData);
printf("raw data size = %d, compressed size = %d\n\n",
inData.getSize(),compressedData.getSize());
// Encrypt the compressed data.
CkByteData encryptedData;
crypt.EncryptBytes(compressedData,encryptedData);
// AES Encryption pads to a multiple of 16 bytes.
printf("Encrypted size = %d\n\n",encryptedData.getSize());
// Decrypt to compressed data.
CkByteData decryptedData;
crypt.DecryptBytes(encryptedData,decryptedData);
CkByteData outData;
crypt.InflateBytes(decryptedData,outData);
outData.appendChar('\0');
printf("Output:\n%s\n",outData.getData());
// PRINTS:
// raw data size = 279658, compressed size = 57568
// Encrypted size = 57584
// Output:
// <?xml version="1.0"?>
// <PLAY>
// <TITLE AUTHOR="William Shakespeare">The Tragedy of Hamlet, Prince of Denmark</TITLE>
// ...
|
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.