Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
AES Encrypt a Byte Array
This sample program demonstrates how to AES encrypt a byte array. // The Chilkat Encryption library is also available as an ActiveX component
// or .NET class with the identical set of methods and properties.
// 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 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);
// Create some data to encrypt.
int i;
unsigned char data[100];
for (i=0; i<100; i++) data[i] = i;
// Encrypt a byte array:
CkByteData inData;
CkByteData encryptedData;
// Borrow the data so an extra memory copy is not needed.
inData.borrowData(data,100);
crypt.EncryptBytes(inData,encryptedData);
// Decrypt the byte array.
CkByteData decryptedData;
crypt.DecryptBytes(encryptedData,decryptedData);
// Print the encrypted and decrypted data.
CkString strTemp;
strTemp.appendHexData(encryptedData.getData(),encryptedData.getSize());
printf("%s\n",strTemp.getString());
strTemp.clear();
strTemp.appendHexData(decryptedData.getData(),decryptedData.getSize());
printf("%s\n",strTemp.getString());
// Prints:
// NOTE: AES encryption pads the encrypted output to a multiple of 16 bytes.
// D814 158C AF6B A38F AE29 672B B4E9 68FD
// EB53 C5D6 FA96 8F43 60BE F85A AAA8 6CA1
// 5E16 6956 6094 12D6 F61A A647 D4DB 698A
// E20F 4DCA 3FBA 5680 8E98 44C2 A08D B191
// 243E DDDA 0669 0D20 4B33 F4E7 EA82 7B63
// A798 D18D 8B54 4BAC 8E8C 340E FDFE 2452
// 0818 0554 9B62 A3DA 2222 81A1 FF52 B776
// 0001 0203 0405 0607 0809 0A0B 0C0D 0E0F
// 1011 1213 1415 1617 1819 1A1B 1C1D 1E1F
// 2021 2223 2425 2627 2829 2A2B 2C2D 2E2F
// 3031 3233 3435 3637 3839 3A3B 3C3D 3E3F
// 4041 4243 4445 4647 4849 4A4B 4C4D 4E4F
// 5051 5253 5455 5657 5859 5A5B 5C5D 5E5F
// 6061 6263
|
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.