小苹果6318 2014-12-10 16:20 采纳率: 50%
浏览 3720
已采纳

那位大神能把我这个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();

}

  • 写回答

3条回答

  • xuzuning 2014-12-11 05:51
    关注
     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);
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题