#include <stdio.h>
#include <string.h>
#include <windows.h>
/* All function indices in QuickCrypt.dll can be found in:*/
/* QuickCrypt Library Help (QuickCrypt Library Run-Time Dynamic Linking page) */
#define DES_INIT_INDEX 1
#define CBC_INIT_INDEX 71
#define CBC_PROCESS_INDEX 74
/* These constants can be found in QuickCrypt.h */
#define DES_BLOCKSIZE 8
#define DES_CONTEXTSIZE 168
#define DES_DEFAULTKEYSIZE 8
#define ENCRYPT 0
#define DECRYPT 1
/* Declare pointer types for each function you need to use */
typedef void ( _stdcall *PDESINIT )( void*, int, const void*, unsigned int );
typedef int ( _stdcall *PCBCINIT )( void*, int, const void*, int );
typedef unsigned int ( _stdcall *PCBCPROCESS )( void*, void*, const void*, unsigned int );
/* performs DES encryption/decryption of memory buffer in CBC mode */
int EncryptDecryptBuffer( unsigned char* buff, const unsigned int buffsize, int dir, const unsigned char* iv, const unsigned char* key )
{
/* dir: ENCRYPT - perform encryption */
/* DECRYPT - perform decryption */
PDESINIT pDESInit;
PCBCINIT pCBCInit;
PCBCPROCESS pCBCProcess;
unsigned char context[DES_CONTEXTSIZE];
int padded = 1; /* true */
/*****Load QuickCrypt.dll*************/
HMODULE hModule = LoadLibrary( "QuickCrypt.dll" );
if( !hModule )
{
printf( "Could not load QuickCrypt.dll library" );
return 1;
}
/*****Get all function addresses*****/
pDESInit = (PDESINIT)GetProcAddress( hModule, (LPCSTR)DES_INIT_INDEX );
pCBCInit = (PCBCINIT)GetProcAddress( hModule, (LPCSTR)CBC_INIT_INDEX );
pCBCProcess = (PCBCPROCESS)GetProcAddress( hModule, (LPCSTR)CBC_PROCESS_INDEX );
/***** Initialize context ****************/
( *pDESInit )( context, dir, key, DES_DEFAULTKEYSIZE );
( *pCBCInit )( context, dir, iv, padded );
/***** Encrypt/Decrypt memory buffer *****/
return ( *pCBCProcess )( context, buff, buff, buffsize ); /* in-place encryption/decryption */
}
void ShowBuffer( const unsigned char* buff, const unsigned int buffsize );
int main()
{
/***** Define key, iv, memory buffer *****/
char key[DES_DEFAULTKEYSIZE] =
{
's', 'e', 'c', 'r', 'e', 't', 'l', 'y'
};
unsigned char iv[DES_BLOCKSIZE] =
{
0x41, 0x3E, 0xF0, 0xA1, 0xC6, 0x11, 0xE5, 0x50
};
unsigned char buff[100] =
{
0xA0, 0x76, 0xEE, 0x97, 0x8F, 0x12, 0x2C, 0x6F,
0xAA, 0x86, 0xC8, 0x13, 0x80, 0x80, 0xEA, 0xC4,
0x6B, 0xF0, 0x0D, 0x4A, 0x6A, 0xE7, 0x6E, 0x09,
0x12, 0x8B, 0xFE
};
unsigned int buffsize = 27;
printf( "Initial memory buffer:\n" );
ShowBuffer( buff, buffsize );
/***** Encrypt memory buffer *************/
buffsize = EncryptDecryptBuffer( buff, buffsize, ENCRYPT, iv, (const unsigned char*)key );
printf( "Encrypted memory buffer:\n" );
ShowBuffer( buff, buffsize );
/***** Decrypt memory buffer *************/
buffsize = EncryptDecryptBuffer( buff, buffsize, DECRYPT, iv, (const unsigned char*)key );
printf( "Decrypted memory buffer:\n" );
ShowBuffer( buff, buffsize );
return 0;
}
void ShowBuffer( const unsigned char* buff, const unsigned int buffsize )
{
unsigned int i = 0;
for( ; i < buffsize; ++i )
{
printf( "%.2x ", (int)buff[ i ] );
if( ( i + 1 ) % DES_BLOCKSIZE == 0 )
printf( "\n" );
}
printf( "\n" );
}
|