Java tutorial
//package com.java2s; //License from project: Apache License import java.nio.charset.Charset; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Main { private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; /** * Encrypt the plain text with MD5 algorithm. * * @param plainText The plain text. * @return The Encrypt content. */ public static String md5Encrypt(String plainText) { try { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(plainText.getBytes(Charset.defaultCharset())); return new String(toHex(digest.digest())); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; } private static char[] toHex(byte[] data) { char[] toDigits = DIGITS_UPPER; int l = data.length; char[] out = new char[l << 1]; // two characters form the hex value. for (int i = 0, j = 0; i < l; i++) { out[j++] = toDigits[(0xF0 & data[i]) >>> 4]; out[j++] = toDigits[0x0F & data[i]]; } return out; } }