Android examples for java.security:MD5
Encode String to MD5 string by converting byte array to hex string
import android.text.TextUtils; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.util.AbstractCollection; import java.util.Collection; import java.util.Iterator; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main{ public static String MD5Encode(String origin) { try {/*from w w w . j av a 2 s . c om*/ MessageDigest md = MessageDigest.getInstance("MD5"); return byteArrayToHexString(md.digest(origin.getBytes())); } catch (Exception ex) { return null; } } public static String byteArrayToHexString(byte[] b) { StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++) { resultSb.append(byteToHexString(b[i])); } return resultSb.toString(); } private static String byteToHexString(byte b) { int n = b; if (n < 0) n = 256 + n; int d1 = n / 16; int d2 = n % 16; return hexDigits[d1] + hexDigits[d2]; } }