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 inTRUEfor the optional$raw_outputargument.Then your Java code has a bug. MD5 hashes are always 128 bits (32 hex digits). Here it is:
this will generate
1instead of01for 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: