Blowfish encryption, respectively, using PHP and C + + implementation, but the results are different. How to handle

Source: Internet
Author: User
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;
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.