Blowfish加密,分别使用PHP和C++实现,但结果不同...
先是MD5实验,结果相同,但使用Blowfish实验,怎么做也成功不了
调用如下:
<?php<br /> <br /> $cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_ECB, '');<br /> <br /> $iv = '00000000';<br /> $key = "strkey11";<br /> <br /> $stext = '38A0E9312DDA8F7C16B9A12159168C76';<br /> $stext = md5($stext, true);<br /> //经过调试知道,在这时$stext的值与C++中MD5后的结果一致<br /> <br /> if (mcrypt_generic_init($cipher, $key, $iv) != -1)<br /> {<br /> $dtext = mcrypt_generic($cipher,$stext );<br /> mcrypt_generic_deinit($cipher);<br /> <br /> // Display the result in hex.<br /> printf("blowfish encrypted:<br>%s<br><br>",strtoupper(bin2hex($dtext)));<br /> }<br /> mcrypt_module_close($cipher);<br />
MD5_CTX md5;<br /> unsigned char str[16];<br /> md5.MD5String(strSource.c_str() ,str);<br /> <br /> BlockCipher *bf;<br /> char key[] = "strkey11"; //Key<br /> bf = new BlowFish();<br /> bf->setKey((void *)key, 8*8);<br /> <br /> bf->encrypt((void *)str, 8); //unsigned char str[16];<br /> bf->encrypt((void *)(str+8), 8);<br /> char temp1[4] = {0};<br /> char buff1[128] = {0};<br /> for(int i = 0;i<16;i++)<br /> {<br /> sprintf(temp1,"%02x",str[i]);<br /> strcat(buff1,temp1);<br /> }<br /> AnsiString strResult = String(buff1).UpperCase();<br /> delete bf;