java php md5,Java和php5 MD5 Hash之间的区别

在将一个使用PHP5 MD5加密的用户数据库迁移至Java平台时,部分用户无法登录,因为Java实现的MD5哈希函数生成的结果不正确。问题出在`byteArrToHexString`方法中,对于字节小于0x10的情况,没有正确地进行零填充。修复方案是在转换前检查字节值,不足两位时前面补零。
摘要由CSDN通过智能技术生成

I'm facing kind of a strange issue which is related MD5-Hashes in Java and php5.

I figured that unter certain circumstances the following code does not

generate correct MD5 hashes:

public static String getMD5Hash(String string)

{

try

{

MessageDigest md5 = MessageDigest.getInstance("MD5");

md5.update(string.getBytes());

byte[] digest = md5.digest();

string = byteArrToHexString(digest);

}

catch (NoSuchAlgorithmException e1)

{

e1.printStackTrace();

}

return string;

}

private static String byteArrToHexString(byte[] bArr)

{

StringBuffer sb = new StringBuffer();

for (int i = 0; i < bArr.length; i++)

{

int unsigned = bArr[i] & 0xff;

sb.append(Integer.toHexString((unsigned)));

}

return sb.toString();

}

I had to migrate an existing user database where the passwords where stored

in php5 MD5. Now some of the users, not all, can't login because my Java code

does not produce the correct MD5 hash.

Any ideas what's wrong with the above?

解决方案

byteArrToHexString does not convert bytes <0x10 correctly, you need to pad them with zeros.

Example:

int unsigned = bArr[i] & 0xff;

if (unsigned < 0x10)

sb.append("0");

sb.append(Integer.toHexString((unsigned)));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值