我知道这可能是一个很长的延伸,但有人能告诉我,我目前在PHP中实现的MD5算法哪里出了问题吗?我只是看起来找不到它出了什么问题。
它返回一个32个字符的十六进制字符串(虽然有25%的情况下它生成的字符串少于32个字符),但是它生成的字符数与内置MD5函数生成的字符数不同。
非常感谢。
<?php
function MD($string){
$a = "67452301";
$b = "EFCDAB89";
$c = "98BADCFE";
$d = "10325476";
$words = init($string);
for($i = 0; $i <= count($words)/16-1; $i++){
$A = $a;
$B = $b;
$C = $c;
$D = $d;
/* ROUND 1 */
FF ($A, $B, $C, $D, $words[0 + ($i * 16)], 7, "d76aa478");
FF ($D, $A, $B, $C, $words[1 + ($i * 16)], 12, "e8c7b756");
FF ($C, $D, $A, $B, $words[2 + ($i * 16)], 17, "242070db");
FF ($B, $C, $D, $A, $words[3 + ($i * 16)], 22, "c1bdceee");
FF ($A, $B, $C, $D, $words[4 + ($i * 16)], 7, "f57c0faf");
FF ($D, $A, $B, $C, $words[5 + ($i * 16)], 12, "4787c62a");
FF ($C, $D, $A, $B, $words[6 + ($i * 16)], 17, "a8304613");
FF ($B, $C, $D, $A, $words[7 + ($i * 16)], 22, "fd469501");
FF ($A, $B, $C, $D, $words[8 + ($i * 16)], 7, "698098d8");
FF ($D, $A, $B, $C, $words[9 + ($i * 16)], 12, "8b44f7af");
FF ($C, $D, $A, $B, $words[10 + ($i * 16)], 17, "ffff5bb1");
FF ($B, $C, $D, $A, $words[11 + ($i * 16)], 22, "895cd7be");
FF ($A, $B, $C, $D, $words[12 + ($i * 16)], 7, "6b901122");
FF ($D, $A, $B, $C, $words[13 + ($i * 16)], 12, "fd987193");
FF ($C, $D, $A, $B, $words[14 + ($i * 16)], 17, "a679438e");
FF ($B, $C, $D, $A, $words[15 + ($i * 16)], 22, "49b40821");
/* ROUND 2 */
GG ($A, $B, $C, $D, $words[1 + ($i * 16)], 5, "f61e2562");
GG ($D, $A, $B, $C, $words[6 + ($i * 16)], 9, "c040b340");
GG ($C, $D, $A, $B, $words[11 + ($i * 16)], 14, "265e5a51");
GG ($B, $C, $D, $A, $words[0 + ($i * 16)], 20, "e9b6c7aa");
GG ($A, $B, $C, $D, $words[5 + ($i * 16)], 5, "d62f105d");
GG ($D, $A, $B, $C, $words[10 + ($i * 16)], 9, "02441453");
GG ($C, $D, $A, $B, $words[15 + ($i * 16)], 14, "d8a1e681");
GG ($B, $C, $D, $A, $words[4 + ($i * 16)], 20, "e7d3fbc8");
GG ($A, $B, $C, $D, $words[9 + ($i * 16)], 5, "21e1cde6");
GG ($D, $A, $B, $C, $words[14 + ($i * 16)], 9, "c33707d6");
GG ($C, $D, $A, $B, $words[3 + ($i * 16)], 14, "f4d50d87");
GG ($B, $C, $D, $A, $words[8 + ($i * 16)], 20, "455a14ed");
GG ($A, $B, $C, $D, $words[13 + ($i * 16)], 5, "a9e3e905");
GG ($D, $A, $B, $C, $words[2 + ($i * 16)], 9, "fcefa3f8");
GG ($C, $D, $A, $B, $words[7 + ($i * 16)], 14, "676f02d9");
GG ($B, $C, $D, $A, $words[12 + ($i * 16)], 20, "8d2a4c8a");
/* ROUND 3 */
HH ($A, $B, $C, $D, $words[5 + ($i * 16)], 4, "fffa3942");
HH ($D, $A, $B, $C, $words[8 + ($i * 16)], 11, "8771f681");
HH ($C, $D, $A, $B, $words[11 + ($i * 16)], 16, "6d9d6122");
HH ($B, $C, $D, $A, $words[14 + ($i * 16)], 23, "fde5380c");
HH ($A, $B, $C, $D, $words[1 + ($i * 16)], 4, "a4beea44");
HH ($D, $A, $B, $C, $words[4 + ($i * 16)], 11, "4bdecfa9");
HH ($C, $D, $A, $B, $words[7 + ($i * 16)], 16, "f6bb4b60");
HH ($B, $C, $D, $A, $words[10 + ($i * 16)], 23, "bebfbc70");
HH ($A, $B, $C, $D, $words[13 + ($i * 16)], 4, "289b7ec6");
HH ($D, $A, $B, $C, $words[0 + ($i * 16)], 11, "eaa127fa");
HH ($C, $D, $A, $B, $words[3 + ($i * 16)], 16, "d4ef3085");
HH ($B, $C, $D, $A, $words[6 + ($i * 16)], 23, "04881d05");
HH ($A, $B, $C, $D, $words[9 + ($i * 16)], 4, "d9d4d039");
HH ($D, $A, $B, $C, $words[12 + ($i * 16)], 11, "e6db99e5");
HH ($C, $D, $A, $B, $words[15 + ($i * 16)], 16, "1fa27cf8");
HH ($B, $C, $D, $A, $words[2 + ($i * 16)], 23, "c4ac5665");
/* ROUND 4 */
II ($A, $B, $C, $D, $words[0 + ($i * 16)], 6, "f4292244");
II ($D, $A, $B, $C, $words[7 + ($i * 16)], 10, "432aff97");
II ($C, $D, $A, $B, $words[14 + ($i * 16)], 15, "ab9423a7");
II ($B, $C, $D, $A, $words[5 + ($i * 16)], 21, "fc93a039");
II ($A, $B, $C, $D, $words[12 + ($i * 16)], 6, "655b59c3");
II ($D, $A, $B, $C, $words[3 + ($i * 16)], 10, "8f0ccc92");
II ($C, $D, $A, $B, $words[10 + ($i * 16)], 15, "ffeff47d");
II ($B, $C, $D, $A, $words[1 + ($i * 16)], 21, "85845dd1");
II ($A, $B, $C, $D, $words[8 + ($i * 16)], 6, "6fa87e4f");
II ($D, $A, $B, $C, $words[15 + ($i * 16)], 10, "fe2ce6e0");
II ($C, $D, $A, $B, $words[6 + ($i * 16)], 15, "a3014314");
II ($B, $C, $D, $A, $words[13 + ($i * 16)], 21, "4e0811a1");
II ($A, $B, $C, $D, $words[4 + ($i * 16)], 6, "f7537e82");
II ($D, $A, $B, $C, $words[11 + ($i * 16)], 10, "bd3af235");
II ($C, $D, $A, $B, $words[2 + ($i * 16)], 15, "2ad7d2bb");
II ($B, $C, $D, $A, $words[9 + ($i * 16)], 21, "eb86d391");
addVars($a, $b, $c, $d, $A, $B, $C, $D);
}
$MD5 = $a.$b.$c.$d;
return $MD5;
}
/* General functions */
function hexbin($str){
$hexbinmap = array("0" => "0000"
, "1" => "0001"
, "2" => "0010"
, "3" => "0011"
, "4" => "0100"
, "5" => "0101"
, "6" => "0110"
, "7" => "0111"
, "8" => "1000"
, "9" => "1001"
, "A" => "1010"
, "a" => "1010"
, "B" => "1011"
, "b" => "1011"
, "C" => "1100"
, "c" => "1100"
, "D" => "1101"
, "d" => "1101"
, "E" => "1110"
, "e" => "1110"
, "F" => "1111"
, "f" => "1111");
$bin = "";
for ($i = 0; $i < strlen($str); $i++)
{
$bin .= $hexbinmap[$str[$i]];
}
$bin = ltrim($bin, '0');
// echo "Original: ".$str." New: ".$bin."<br />";
return $bin;
}
function strhex($str){
$hex = "";
for ($i = 0; $i < strlen($str); $i++)
{
$hex = $hex.dechex(ord($str[$i]));
}
return $hex;
}
/* MD5-specific functions */
function init($string){
$len = strlen($string);
$hex = strhex($string); // convert ascii string to hex
$bin = hexbin($hex); // convert hex string to bin
$padded = pad($bin);
$padded = pad($padded, 1, $len);
$block = str_split($padded, 32);
return $block;
}
function pad($bin, $type=0, $len = 0){
if($type == 0){
$bin = $bin."1";
$buff = strlen($bin) % 512;
if($buff != 448){
while(strlen($bin) % 512 != 448){
$bin = $bin."0";
}
}
}
// append length (b) of string to latter 64 bits
elseif($type == 1){
$bLen = decbin($len);
if(strlen($bLen) > 64){
$words = truncate64($bLen);
$bin .= $words[1].$words[0];
}
else{
while(strlen($bLen) < 64){
$bLen .= "0";
}
$words = str_split ($bLen, 32);
$bin .= $words[1].$words[0];
}
}
return $bin;
}
function truncate64($string){
$trunc = substr($string, strlen($string) - 64, 64);
$trunc = str_split ($trunc, 32);
return $trunc;
}
/* MD5 base functions */
function F($X, $Y, $Z){
$X = hexbin($X);
$Y = hexbin($Y);
$Z = hexbin($Z);
$calc = ($X & $Y) | ((~ $X) & $Z); // X AND Y OR NOT X AND Z
$calc = bindec($calc);
return $calc;
}
function G($X, $Y, $Z){
$X = hexbin($X);
$Y = hexbin($Y);
$Z = hexbin($Z);
$calc = ($X & $Z) | ($Y & (~ $Z)) ; // X AND Z OR Y AND NOT Z
$calc = bindec($calc);
return $calc;
}
function H($X, $Y, $Z){
$X = hexbin($X);
$Y = hexbin($Y);
$Z = hexbin($Z);
$calc = $X ^ $Y ^ $Z; // X XOR Y XOR Z
$calc = bindec($calc);
return $calc;
}
function I($X, $Y, $Z){
$X = hexbin($X);
$Y = hexbin($Y);
$Z = hexbin($Z);
$calc = $Y ^ ($X | (~ $Z)) ; // Y XOR (X OR NOT Z)
$calc = bindec($calc);
return $calc;
}
/* MD5 round functions */
/*
$A - hex, $B - hex, $C - hex, $D - hex (F - dec)
$M - binary
$s - decimal
$t - hex
*/
function FF(&$A, $B, $C, $D, $M, $s, $t){
$A = hexdec($A);
$t = hexdec($t);
$M = bindec($M);
$A = hexdec($B) + (($A + F($B, $C, $D) + $M + $t)); //decimal
$A = rotate($A, $s);
}
function GG(&$A, $B, $C, $D, $M, $s, $t){
$A = hexdec($A);
$t = hexdec($t);
$M = bindec($M);
$A = hexdec($B) + (($A + G($B, $C, $D) + $M + $t)); //decimal
$A = rotate($A, $s);
}
function HH(&$A, $B, $C, $D, $M, $s, $t){
$A = hexdec($A);
$t = hexdec($t);
$M = bindec($M);
$A = hexdec($B) + (($A + H($B, $C, $D) + $M + $t)); //decimal
$A = rotate($A, $s);
}
function II(&$A, $B, $C, $D, $M, $s, $t){
$A = hexdec($A);
$t = hexdec($t);
$M = bindec($M);
$A = hexdec($B) + (($A + I($B, $C, $D) + $M + $t)); //decimal
$A = rotate($A, $s);
}
// shift
function rotate($decimal, $bits) { //returns hex
$binary = decbin($decimal);
$shifted = substr($binary, $bits).substr($binary, 0, $bits);
$hexshift = base_convert($shifted, 2, 16);
return $hexshift;
}
function addVars(&$a, &$b, &$c, &$d, $A, $B, $C, $D){
$A = hexdec($A);
$B = hexdec($B);
$C = hexdec($C);
$D = hexdec($D);
$aa = hexdec($a);
$bb = hexdec($b);
$cc = hexdec($c);
$dd = hexdec($d);
$aa = $aa + $A;
$bb = $bb + $A;
$cc = $cc + $A;
$dd = $dd + $A;
$a = dechex($aa);
$b = dechex($bb);
$c = dechex($cc);
$d = dechex($dd);
}
?>
4条答案
按热度按时间igetnqfo1#
出于某种原因,这个问题并没有让我一个人呆着,所以我仔细检查了你的代码,并修复了错误,直到它工作:
在你开始之前,我有两点建议:
1.不要在int值和hex/bin表示之间来回转换;在进行任何处理之前转换为int值;使代码更具可读性。
1.使用
call_user_func()
,只执行一次GG-〉G、II-〉I函数。此外,还有一个微妙的错误仍然存在;仅由零个字符组成的输入串将不能被正确编码;我将把修复它作为一个练习留给读者:-)。
init()
中:附加的是未填充消息的长度,以位为单位,而不是字符数:
此外,您必须填充从
hexbin
获得的内容,否则后续对str_split
的调用将得到错误的对齐:字节顺序也是小端序:
strhex()
中:有很多这样的填充错误;
dechex(ord("\1"))
是'1'
而不是'01'
:pad()
中:truncate64()
完全不符合Map:-):位运算符不能用于二进制字符串:
您在旋转前添加
$B
,必须在旋转后添加;另外,由于你在string和int表示之间来回转换,并且PHP_INT_SIZE
可能大于4(例如在64位平台上),所以你必须确保你只使用低32位:addVars()
中:$A
对于每次添加重复,可能是复制粘贴伪影:-):rotate()
中:在你的rotate函数中有一个填充错误(又一次)。把它扔掉,替换成:
MD()
中:最后但并非最不重要的一点是,您必须再次转换为little endian:
缺少
leftpad()
函数:完整编辑来源:
2guxujil2#
很高兴你尝试了!我有过类似的经历,很久以前我用Tcl实现了一个MD5算法。我发现调试它的最好方法是逐行跟踪,知道应该执行什么操作,并通过手工计算确定是否确实执行了正确的操作。
这个问题没有简单的答案,而且如果没有详细的分析,也不可能从您发布的代码中判断出可能的错误。
(我假设您已经了解标准的md5()函数,并且您这样做是为了学习。)
euoag5mw3#
如果代码对您不起作用,请使用以下代码:
z4bn682m4#
这是我的版本,简化了主迭代