java密码转换,将密码加密从java转换为php

I'm trying to create a PHP version of an existing JSP program, however I'm stuck at the password encryption part.

Could you please tell me how to convert this one? I know it tries to get the md5() but after that, I don't get it. I get lost in the Stringbuffer and for() parts.

Can you help me out?

public static String encryptPassword( String password )

{

String encrypted = "";

try

{

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

byte[] passwordBytes = password.getBytes( );

digest.reset( );

digest.update( passwordBytes );

byte[] message = digest.digest( );

StringBuffer hexString = new StringBuffer();

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

{

hexString.append( Integer.toHexString(

0xFF & message[ i ] ) );

}

encrypted = hexString.toString();

}

catch( Exception e ) { }

return encrypted;

}

解决方案

Iraklis should be right. md5() gives you a hex-encoded output string by default. You only get the unencoded bytes like in Java by passing in TRUE for the optional $raw_output argument.

the lengths range from 29 to 32

Then your Java code has a bug. MD5 hashes are always 128 bits (32 hex digits). Here it is:

hexString.append( Integer.toHexString(0xFF & message[ i ] ) );

this will generate 1 instead of 01 for all bytes below 16. What you have stored is a mangled hash, from which you cannot recover the original MD5 value. If you absolutely must keep this broken data, you will have to reproduce the bug in PHP:

function makeBrokenMD5($s) {

$hash= md5($s, TRUE);

$bytes= preg_split('//', $hash, -1, PREG_SPLIT_NO_EMPTY);

$broken= '';

foreach ($bytes as $byte)

$broken.= dechex(ord($byte));

return $broken;

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值