Java Encrypt String using MD5

To get the md5 hash equivalent of a String, use this method. Remember, that there’s no way to undo an md5 hash conversion.

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
public static String hex(byte[] array) {
  StringBuffer sb = new StringBuffer();
  for (int i = 0; i < array.length; ++i) {
    sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).toUpperCase().substring(1,3));
  }
  return sb.toString();
}
 
 
public static String md5(String message) { 
  try { 
    MessageDigest md = MessageDigest.getInstance("MD5"); 
    return hex (md.digest(message.getBytes("CP1252"))); 
  } catch (NoSuchAlgorithmException e) { } catch (UnsupportedEncodingException e) { } 
  return null;
}

Donations appreciated. Every little $ helps. Or click Google +1. Or play the short video on the upper right of this page.

Related Posts Plugin for WordPress, Blogger...