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;
}
I am using Java. I used this method for hashing the password and it correctly worked while storing into the database. Now I have difficulty with decryption. Are there any methods more effective than this one?
You can’t decrypt it. MD5 is a hash – it’s one way, unlike a two-way encryption algorithm.
You generally shouldn’t be trying to decrypt passwords though – you store the hash (salted, ideally) and then compare the “known good” hash with the hash of the password given to you by the user later.
(I would avoid MD5 these days personally, but that’s another story.)