Java Encrypt String using MD5

(1 votes, average: 5.00 out of 5)
 Loading ...

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
}

Found this post useful? Buy me a cup of coffee or help support the sponsors on the right.

Related Posts with Thumbnails

tags: , ,

One Response to “Java Encrypt String using MD5”

  1. 1
    Deliciosa Says:

    Bro, you forgot to leave your links in my blog. btw, thank you for the link.

Leave a Reply

Spam protection by WP Captcha-Free