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

php中文网
发布: 2016-06-02 11:33:55
原创
929人浏览过

javaphp加密md5

继续求助第二波,那位大神能这个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 {
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 {
int current = input[i] & 0xff;
if (current 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 $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);
}

Spirit Me
Spirit Me

SpiritMe允许用户使用数字化身制作视频,这些化身可以模拟用户的声音和情感

Spirit Me 178
查看详情 Spirit Me
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号