1 import java.security.MessageDigest; 2 3 /** 4 * md5加密 <功能详细描述> 5 * 6 * @author wanglei 7 * @version [版本号, Feb 28, 2011] 8 * @see [相关类/方法] 9 * @since WihomeV100R001C02LGDM05 10 */ 11 public class Md5Util 12 { 13 private static final String[] HEXDIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", 14 "e", "f"}; 15 16 private static final int NUM256 = 256; 17 18 private static final int NUM16 = 16; 19 20 /** 21 * 对字符串进行MD5加密 <功能详细描述> 22 * 23 * @param originString String 24 * @return String 25 * @see [类、类#方法、类#成员] 26 */ 27 public static String generatePassword(String originString) 28 { 29 if (originString != null) 30 { 31 try 32 { 33 MessageDigest md = MessageDigest.getInstance("MD5"); 34 // 使用指定的字节数组对摘要进行最后更新,然后完成摘要计算 35 byte[] results = md.digest(originString.getBytes()); 36 // 将得到的字节数组变成字符串返回 37 String resultString = byteArrayToHexString(results); 38 return resultString; 39 } 40 catch (Exception ex) 41 { 42 ex.printStackTrace(); 43 } 44 } 45 return null; 46 } 47 48 private static String byteArrayToHexString(byte[] b) 49 { 50 StringBuffer resultSb = new StringBuffer(); 51 for (int i = 0; i < b.length; i++) 52 { 53 resultSb.append(byteToHexString(b[i])); 54 } 55 return resultSb.toString(); 56 } 57 58 /** 59 * 将一个字节转化成十六进制形式的字符串 <功能详细描述> 60 * 61 * @param b byte 62 * @return String 63 * @see [类、类#方法、类#成员] 64 */ 65 private static String byteToHexString(byte b) 66 { 67 int n = b; 68 if (n < 0) 69 { 70 n = NUM256 + n; 71 } 72 int d1 = n / NUM16; 73 int d2 = n % NUM16; 74 return HEXDIGITS[d1] + HEXDIGITS[d2]; 75 } 76 } 功能详细描述> 功能详细描述> 功能详细描述>