#include <iostream>
#include <QuickCrypt.h>
using namespace std;
using namespace QuickCrypt;
// performs DESX 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 CDESX object
CDESX cipher( dir, key );
//Encrypt/Decrypt memory buffer
for( int i = 0; i < numblocks; ++i )
{
cipher.ProcessBlock( buff + i * CDESX::BLOCKSIZE ); //in-place encryption/decryption
}
}
void ShowBuffer( const unsigned char* buff, const unsigned int numblocks );
int main()
{
//Define key & blocks
char key[CDESX::DEFAULTKEYSIZE] =
{
's', 'e', 'c', 'r', 'e', 't', 'l', 'y',
'2', '9', '5', '1', '7', '4', '%', '*',
'^', '&', ')', '2', '$', ')', '-', '7'
};
const unsigned int NUM_BLOCKS = 3;
unsigned char blocks[CDESX::BLOCKSIZE * NUM_BLOCKS] =
{
0xA6, 0x7E, 0x40, 0x80, 0x0F, 0xCF, 0x24, 0xC5,
0x8C, 0x5B, 0x3C, 0x42, 0xDE, 0xB1, 0x11, 0xA0,
0x97, 0xE9, 0xA7, 0x87, 0x99, 0x91, 0x1B, 0x3B
};
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 < CDESX::BLOCKSIZE; ++j )
{
cout.fill( '0' );
cout.width( 2 );
cout << (int)buff[ i * CDESX::BLOCKSIZE + j ] << " ";
}
cout << endl;
}
}
|