Using cryptopp (aka crypto++) and mcrypt together
OK don't know what happened to my post it was all screwed up could be my
work account
so now from home I've set my options to plain text, hopefully it will be ok.
Having not seen much in the way of complete implementations
of cryptopp and mcrypt together here is a solution I am using for 3DES
encryption that works well. I initially tried using MS CryptoAPI to
mcrypt and it was a nightmare, Using Cryptopp worked
out great for me.
I dont profess that this code is
optimized, It is not there probably is a better or neater way to do this
if you have any suggestions please reply to the list.
//////////HERE IS THE CLIENT CODE WITH A TEST METHOD////////////////////
BYTE * Crypto::EncryptData(BYTE plaintext[])
{
byte * ciphertext;
byte * result;
int inputLength;
int outputLength;
//Key is just the first 24 BYTES (Got 24Bytes from
DES_EDE3::DEFAULT_KEYLENGTH)
//of an md5 hash of whatever you want to use for instance I just used
the
word
//"monkey" got it's md5 hash from the server and then plugged in the
first
//24 BYTES into const byte key [],
//dont use const byte key[DES_EDE3::DEFAULT_KEYLENGTH] as Visual
studio
//will make the last byte a null only leaving you 23Bytes of keyspace
//which then wont work. Keeping things simple I have made
//IV is just 8 bytes of data taken from the key.
const byte key [] = "d0763edaa9d9bd2a9516280e";
const BYTE iv [] = "d0763eda";
//Setup encryptor with specs and options *using ECB ,iv set and
//pad w/ zeros, MCRYPT uses zeros for padding not pkcs5
ECB_Mode::Encryption ecbEncryption(key, DES_EDE3::DEFAULT_KEYLENGTH,
iv);
StreamTransformationFilter encryptor(ecbEncryption, NULL,
StreamTransformationFilter::ZEROS_PADDING);
//Get length of input and feed it into the encryptor then
//tell it all done feeding w/MessageEnd
inputLength = strlen((const char *)plaintext);
encryptor.Put(plaintext, inputLength);
encryptor.MessageEnd();
//Findout how big the ciphertext is and create a buffer
//samesize, then call Get to put ciphertext in it.
outputLength = encryptor.MaxRetrievable();
ciphertext = new byte[outputLength];
encryptor.Get(ciphertext, outputLength);
return ciphertext;
}
BYTE * Crypto::DecryptData(BYTE * ciphertext)
{
byte * result;
int inputLength;
int outputLength;
const byte key [] = "d0763edaa9d9bd2a9516280e";
const BYTE iv[] = "d0763eda";
//Setup decryptor with specs and options *using ECB ,iv
//set and pad w/ zeros MCRYPT uses zeros for padding not pkcs5
ECB_Mode::Decryption ecbDecryption(key, DES_EDE3::DEFAULT_KEYLENGTH,
iv);
StreamTransformationFilter decryptor(ecbDecryption, NULL,
StreamTransformationFilter::ZEROS_PADDING);
//Get length of ciphertext and feed it into the decryptor
//then tell it all done feeding w/MessageEnd
inputLength = strlen((const char *)ciphertext);
decryptor.Put(ciphertext, inputLength);
decryptor.MessageEnd();
//Findout how big the plaintext is then, create a buffer
//of samesize, then call Get to put plaintext in it.
outputLength = decryptor.MaxRetrievable();
result = new byte[outputLength];
decryptor.Get(result, outputLength);
return result;
}
int Crypto::CryptTest()
{
byte plaintext[] ="ABCDEFGHIJKLMNOPQRSTUVWXYZFFGGHHISRRTTUUVVWWX";
printf("This is plaintext %s\n",plaintext);
byte * Cipher = EncryptData(plaintext);
printf("This is the cipher %s\n", Cipher);
byte * Decode = DecryptData(Cipher);
printf("This is the Decrypted text %s\n", Decode);
return 0;
}
////////NOW On to the MCRYPT Server using php//////////////////////////
//PHPCODE
$passphrase ="monkey";
function EncryptData($input) {
$ALG = MCRYPT_3DES;
$MODE = MCRYPT_MODE_ECB;
$td = mcrypt_module_open($ALG, '', $MODE, '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
$ks = mcrypt_enc_get_key_size($td);
$key = substr(md5($passphrase), 0, $ks);
//could also use this $iv = substr(md5($passphrase), 0, 8);
mcrypt_generic_init($td, $key, $iv);
$encrypted_data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $encrypted_data;
}
function DecryptData($encrypted_data) {
$ALG = MCRYPT_3DES;
$MODE = MCRYPT_MODE_ECB;
$td = mcrypt_module_open($ALG, '', $MODE, '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
$ks = mcrypt_enc_get_key_size($td);
$key = substr(md5($passphrase), 0, $ks);
//could also use this $iv = substr(md5($passphrase), 0, 8);
mcrypt_generic_init($td, $key, $iv);
$decrypted_data = mdecrypt_generic($td, $encrypted_data);
$decrypted_data = trim($decrypted_data);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $decrypted_data;
}
function TestEncryption() {
$Tstring = "ThisIsAtString";
$Ecrypt = EncryptData($Tstring);
$Dcrypt = DecryptData($Ecrypt);
printf("This is the plain text <strong>%s</strong><br><br>", $Tstring);
printf("This is the cipher text <strong>%s</strong><br><br>", $Ecrypt);
printf("This is the Decrypted Text <strong>%s</strong><br><br>",
$Dcrypt);
}
There you go hope this makes someone's life alittle easier
took me a week of trial and error to get everything
up and going.
AJ