#include <iostream>
#include <QuickCrypt.h>
using namespace std;
using namespace QuickCrypt;
// performs Rijndael (AES) encryption/decryption of memory buffer in ECB mode
void EncryptDecryptBuffer( unsigned char* buff, const unsigned int numblocks, SL_CIPHER_DIR dir, const unsigned char* key )
{
//dir: SLC_ENCRYPT - perform encryption
// SLC_DECRYPT - perform decryption
//Instantiate CRijndael object
CRijndael cipher( dir, key );
//Encrypt/Decrypt memory buffer
for( int i = 0; i < numblocks; ++i )
{
cipher.ProcessBlock( buff + i * CRijndael::BLOCKSIZE ); //in-place encryption/decryption
}
}
void ShowBuffer( const unsigned char* buff, const unsigned int numblocks );
int main()
{
//Define key & blocks
char key[CRijndael::DEFAULTKEYSIZE] =
{
's', 'e', 'c', 'r', 'e', 't', 'l', 'y',
'2', '9', '5', '1', '7', '4', '%', '*'
};
const unsigned int NUM_BLOCKS = 3;
unsigned char blocks[CRijndael::BLOCKSIZE * NUM_BLOCKS] =
{
0x73, 0x9B, 0xAB, 0xA6, 0xD7, 0xCC, 0x6A, 0xD7, 0x11, 0x32, 0x8D, 0xEC, 0xC6, 0x57, 0xE7, 0xE5,
0xE6, 0xD9, 0xBB, 0x7A, 0x8F, 0x59, 0xE5, 0x12, 0x25, 0x61, 0x69, 0x90, 0x9A, 0xE4, 0x6D, 0xB5,
0x9E, 0x6E, 0xCC, 0xAE, 0x5B, 0x63, 0xCF, 0xE1, 0x42, 0xF0, 0x90, 0x39, 0xD4, 0xCF, 0x85, 0x87
};
cout.setf( ios::hex, ios::basefield );
cout << "Initial blocks:" << endl;
ShowBuffer( blocks, NUM_BLOCKS );
//Encrypt blocks
EncryptDecryptBuffer( blocks, NUM_BLOCKS, SLC_ENCRYPT, (const unsigned char*)key );
cout << "Encrypted blocks:" << endl;
ShowBuffer( blocks, NUM_BLOCKS );
//Decrypt blocks
EncryptDecryptBuffer( blocks, NUM_BLOCKS, SLC_DECRYPT, (const unsigned char*)key );
cout << "Decrypted blocks:" << endl;
ShowBuffer( blocks, NUM_BLOCKS );
return 0;
}
void ShowBuffer( const unsigned char* buff, const unsigned int numblocks )
{
for( int i = 0; i < numblocks; ++i )
{
for( int j = 0; j < CRijndael::BLOCKSIZE; ++j )
{
cout.fill( '0' );
cout.width( 2 );
cout << (int)buff[ i * CRijndael::BLOCKSIZE + j ] << " ";
}
cout << endl;
}
}
|