小苹果6318 2014-12-12 16:17 采纳率: 50%
浏览 2580
已结题

继续求助第二波,那位大神能把这个Java md5的方法写个php版的,得加密结果一致的,谢谢了!

继续求助第二波,那位大神能这个Java md5的方法写个php版的,得加密结果一致的,谢谢了,第一波有位达人写了一个了,但是加密结果不一样,大家可以参考下!
java方法描述的思路如下
1.将秘钥、源串分别转换byte数组
2.声明2个64位数组 将key的byte数组分别做异或运算填充进去 并分别补充 54、92 补满64长度
3.获得md5摘要算法的MessageDigest 对象
4.使用其中一个数组及源串的数组更新MessageDigest 摘要 完成哈希计算
5.重置摘要
6.使用另一个数组更新摘要 使用4中结果 从0到16开始更新摘要 完成哈希计算
7.转换字符串
方法如下
public String cryptMd5(String source, String key) {
byte[] k_ipad = new byte[64];
byte[] k_opad = new byte[64];
byte[] keyb;
byte[] value;
try { byte[] keyb = key.getBytes("UTF-8");
value = source.getBytes("UTF-8");
}
catch (UnsupportedEncodingException e)
{
byte[] value;
keyb = key.getBytes();
value = source.getBytes();
}
Arrays.fill(k_ipad, keyb.length, 64, 54);
Arrays.fill(k_opad, keyb.length, 64, 92);
for (int i = 0; i < keyb.length; i++)
{
k_ipad[i] = (byte)(keyb[i] ^ 0x36);
k_opad[i] = (byte)(keyb[i] ^ 0x5C);
}
MessageDigest md = null;
try
{
md = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException e)
{
return null;
}
md.update(k_ipad);
md.update(value);
byte[] dg = md.digest();
md.reset();
md.update(k_opad);
md.update(dg, 0, 16);
dg = md.digest();
return toHex(dg); }
public static String toHex(byte[] input)
{
if (input == null) {
return null;
}
StringBuffer output = new StringBuffer(input.length * 2);
for (int i = 0; i < input.length; i++)
{
int current = input[i] & 0xFF;
if (current < 16)
output.append("0");
output.append(Integer.toString(current, 16));
}
return output.toString();
}

下面这个是上次提问达人写的,加密结果和Java的还是不一致
function cryptMd5($source, $key) {
if(! mb_check_encoding($source, 'utf-8')) $source = mb_convert_encoding($source, "utf-8", "auto");
if(! mb_check_encoding($key, 'utf-8')) $key = mb_convert_encoding($key, "utf-8", "auto");
$k_ipad = str_pad($key, 64, chr(54));

$k_opad = str_pad($key, 64, chr(92));
for($i=0; $i<strlen($key); $i++) {
$k_ipad{$i} = $key{$i} ^ chr(0x36);
$k_opad{$i} = $key{$i} ^ chr(0x5c);
}
$dg = md5($source . substr($k_ipad, strlen($source)), true);
$dg = md5(substr($dg, 0, 16) . substr($k_opad, 16), true);
return bin2hex($dg);
}

  • 写回答

3条回答 默认 最新

  • devmiao 2014-12-12 16:58
    关注

    参考:<?php

    //php为正确的 结果:CA15B8C6D72A4442942045956DD371F8
    $password="123456";
    $pt='\x00\x00\x00\x00\x16\x9d\x56\x75';
    $vc='!PRY';
    $passwd = jspassword($password,$pt, $vc);
    echo "
    结果:".$pt."
    ".$vc."
    密码:".$passwd."

    ";

    function jspassword($p,$pt,$vc,$md5 = true)
    {
    echo $p.":".$pt.":".$vc;
    if($md5)
    {
    $p = strtoupper(md5($p));
    }
    //echo "
    ".$p;exit;
    $len = strlen($p);
    $temp = null;
    //echo "
    md5Password:".$p."
    ";
    for ($i=0; $i < $len ; $i = $i + 2)
    {
    //echo "
    i:".$i;
    $temp .= '\x'.substr($p, $i,2);
    }
    //echo "
    ".$temp."
    ";
    //echo "


    --->>>".md5(hex2asc($temp).hex2asc($pt));
    //$str=hex2asc($temp).hex2asc($pt);
    //echo "
    内部:".$str."-->".md5(hex2asc($temp))."-->".md5(hex2asc($pt));
    return strtoupper(md5(strtoupper(md5(hex2asc($temp).hex2asc($pt))).$vc));
    }

    /**

    • 十六进制转字符
    • @access private
    • @param string $str
    • @return string */ function hex2asc($str) { //echo "处理前:".$str."
      "; //print_r( explode('\x', $str)); $str = join('', explode('\x', $str)); //echo "
      处理后:".$str; $len = strlen($str); $data = null; for ($i=0;$i<$len;$i+=2) { //echo "
      ::".substr($str,$i,2); echo "
      ".hexdec(substr($str,$i,2)).":::".chr(hexdec(substr($str,$i,2)))."->".md5(chr(hexdec(substr($str,$i,2)))); $data.=chr(hexdec(substr($str,$i,2))); } echo "
      ".md5($data)."
      "; return $data; } ?>
    评论

报告相同问题?

悬赏问题

  • ¥15 变数的长度不一样”。是什么原因呢?如何修改?
  • ¥15 matlab作业不会写
  • ¥15 vue2(标签-chrome|关键词-浏览器兼容)
  • ¥15 python网络流自动生成系统 医学领域
  • ¥15 sql查询仓库里都有什么
  • ¥15 代码的修改,添加和运行完善
  • ¥15 krpano-场景分组和自定义地图分组
  • ¥15 lammps Gpu加速出错
  • ¥15 关于PLUS模型中kapaa值的问题
  • ¥15 关于博途V17进行仿真时无法建立连接问题