我一直在尝试用 PHP 制作 3DES 算法。我用 Java 做的,效果很好,但是 PHP 版本给了我不同的结果;这是我的代码:
function String2Hex($string){
$hex='';
for ($i=0; $i < strlen($string); $i++){
$hex .= dechex(ord($string[$i]));
}
return $hex;
}
function hexToAscii($inputHex) {
$inputHex = str_replace(' ', '', $inputHex);
$inputHex = str_replace('\x', '', $inputHex);
$ascii = pack('H*', $inputHex);
return $ascii;
}
$cipher = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
$iv = '0000000000000000';
$key = '75ABFD405D018A9BD0E66D23DA3B6DC8';
printf("KEY: %s\n", String2Hex($key));
$cleartext = '0436A6BFFFFFFFA8';
printf("<br>TEXT: %s\n\n", $cleartext);
if (mcrypt_generic_init($cipher, hexToAscii($key), $iv) != -1)
{
$cipherText = mcrypt_generic($cipher, hexToAscii($cleartext));
mcrypt_generic_deinit($cipher);
printf("<br><br>3DES encrypted:\n%s\n\n", strtoupper(bin2hex($cipherText)));
}
它必须给我:76FB62FB3AFD6677
但它给了我:E01BD1085F0126A2
我能做些什么?