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 string returning an encrypted string.
This sample program demonstrates how to encrypt a string using 256-bit AES encryption. // 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 string returning an encrypted string.
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 256-bit AES encryption.
crypt.put_KeyLength(256);
// 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 32 bytes (32 bytes * 8 bits/byte = 256 bits)
// Example:
// int i;
// unsigned char sKey[32];
// for (i=0; i<32; i++) sKey[i] = i;
// secretKey.append(sKey,32);
// Tell the component what string-encoding to use for the encrypted output.
// Encrypted data is binary, and will not be a printable string. If you want
// a printable string, it must be encoded using Base64, Hex, Quoted-Printable, etc.
crypt.put_EncodingMode("base64");
// Encrypt a string:
// Note: The AES algorithm pads to a multiple of 16 bytes, so the encrypted
// (binary) output will be a multiple of 16 bytes. In this case, the encrypted
// data is then base64-encoded. Base64 encoding increases the output length by
// 4/3rds (4 characters for every 3 bytes encoded).
CkString strEncrypted;
crypt.EncryptStringENC("This is a test",strEncrypted);
printf("strEncrypted = [%s]\n",strEncrypted.getString());
// Now decrypt it:
CkString strDecrypted;
crypt.DecryptStringENC(strEncrypted.getString(),strDecrypted);
printf("strDecrypted = [%s]\n",strDecrypted.getString());
// Prints:
// strEncrypted = [Tt8mImCTz77f7fbbKhEU6A==]
// strDecrypted = [This is a test]
}
|
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.