Blowfish encryption, respectively, using PHP and C + + implementation, but the results are different ...
First MD5 experiment, the result is the same, but use Blowfish experiment, how to do also can not succeed
The call is as follows:
$cipher = Mcrypt_module_open (Mcrypt_blowfish, ', MCRYPT_MODE_ECB, ');
$iv = ' 00000000 ';
$key = "Strkey11";
$stext = ' 38a0e9312dda8f7c16b9a12159168c76 ';
$stext = MD5 ($stext, true);
After debugging, you know, at this point the value of $stext is consistent with the result of MD5 in C + +
if (Mcrypt_generic_init ($cipher, $key, $iv)! =-1)
{
$dtext = Mcrypt_generic ($cipher, $stext);
Mcrypt_generic_deinit ($cipher);
Display the result in hex.
printf ("Blowfish Encrypted:
%s
", Strtoupper (Bin2Hex ($dtext)));
}
Mcrypt_module_close ($cipher);
C + + is the case:
Md5_ctx MD5;
unsigned char str[16];
MD5. Md5string (STRSOURCE.C_STR (), str);
Blockcipher *BF;
Char key[] = "Strkey11"; Key
BF = new BlowFish ();
Bf->setkey (void *) key, 8*8);
Bf->encrypt ((void *) STR, 8); unsigned char str[16];
Bf->encrypt (void *) (str+8), 8);
Char Temp1[4] = {0};
Char buff1[128] = {0};
for (int i = 0;i<16;i++)
{
sprintf (Temp1, "%02x", Str[i]);
strcat (BUFF1,TEMP1);
}
Ansistring strresult = String (buff1). Uppercase ();
Delete BF;
------Solution--------------------
$iv = ' 00000000 ';???
Press Bf->setkey (void *) key, 8*8); Understand
Should be
$iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
, huh?
------Solution--------------------
IV is ignored in ECB. IV must exist in CFB, CBC, STREAM, NOFB and OFB modes.
The MCRYPT_MODE_ECB pattern, $iv is ignored, should not be the problem.
It's like padding before encryption, you try.
$size = Mcrypt_get_block_size (Mcrypt_blowfish, MCRYPT_MODE_ECB);
$input = Pkcs5_pad ($input, $size);
function Pkcs5_pad ($text, $blocksize)
{
$pad = $blocksize-(strlen ($text)% $blocksize);
Return $text. Str_repeat (Chr ($pad), $pad);
}
function Pkcs5_unpad ($text)
{
$pad = Ord ($text {strlen ($text)-1});
if ($pad > strlen ($text)) return false;
if (strspn ($text, Chr ($pad), strlen ($text)-$pad)! = $pad) return false;