用php转换的Java消息摘要更新
我用Java编写了这段代码。很抱歉使用了pastebin链接,但它确实允许我粘贴那么多代码 哪个输出“Ap1pAXP8yS” 我正在尝试用PHP转换代码。到目前为止,我已经转换了一些,但它不能正常工作(不能在PHP中获得Ap1pAXP8yS) 主要问题似乎是md.update 下面是PHP中的代码用php转换的Java消息摘要更新,java,php,Java,Php,我用Java编写了这段代码。很抱歉使用了pastebin链接,但它确实允许我粘贴那么多代码 哪个输出“Ap1pAXP8yS” 我正在尝试用PHP转换代码。到目前为止,我已经转换了一些,但它不能正常工作(不能在PHP中获得Ap1pAXP8yS) 主要问题似乎是md.update 下面是PHP中的代码 function bin2print($hex) { $out = ""; $mapping = [ '0', '1', '2', '3', '4', '5', '6', '7', '
function bin2print($hex)
{
$out = "";
$mapping = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '$', 'j', 'k', '-', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', '=', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '.', '/', '%' ];
for ($i = 0; $i < count($hex); $i++) {
$j = $hex[$i];
if ($j < 0) {
$j = 256 + $j;
}
$out.= $mapping [($j % count($mapping)) ];
}
return $out;
}
function byteStr2byteArray($s)
{
return array_slice(unpack("C*", "\0".$s), 1);
}
function getBytes($string)
{
$bytes = array();
for($i = 0; $i < strlen($string); $i++) {
$bytes[] = ord($string[$i]);
}
$out = '';
foreach($bytes as $single) {
$out.=$single ;
}
return ($out);
}
$serial = '111111111111';
$header = 'cow';
$ctx = hash_init('md5' );
hash_update($ctx, getBytes($header));
hash_update($ctx, getBytes($serial));
$raw = hash_final($ctx,true);
$out = bin2print(byteStr2byteArray($raw));
echo substr($out,0,10);
函数bin2print($hex)
{
$out=“”;
$mapping=['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','g','h','j','k','k','-','m','n','n','o','p','s','p','s','s','t','s','u','v','s','x','z','a','b','c','d','e','f','g','g','h','r','s','s','t','u','v','w','x','y','y','y',';
对于($i=0;$i
Wich输出“-fjk7WCU2E”
我做错了什么?有几个bug,但最重要的是你把事情复杂化了。只需使用base64编码和内置函数。在PHP方面:
$ctx = hash_init('md5');
hash_update($ctx, $header);
hash_update($ctx, $serial);
$raw = hash_final($ctx, true);
$out = substr(base64_encode($raw), 0, 10);
在Java方面:
// ...
raw = md.digest();
out = Base64.getEncoder().encodeToString(raw).substring(0, 10);