1 public class Md5Util {
2 /**
3 * @author lwl
4 * @param message 需要加密的字符串
5 * @return md5加密后的字符串
6 */
7 public static String getMd5(String message){
8 String md5str = "";
9 try {
10 MessageDigest md = MessageDigest.getInstance("MD5");
11 byte[] input = message.getBytes();
12 byte[] buff = md.digest(input);
13 md5str = bytesToHex(buff);
14 } catch (Exception e) {
15 e.printStackTrace();
16 }
17 return md5str.toLowerCase(); //将返回的加密字符串转换成小写
18 }
19 public static String bytesToHex(byte[] bytes){
20
21 StringBuffer md5str = new StringBuffer();
22 //把数组每一字节换成16进制连成md5字符串
23 int digital;
24 for (int i = 0; i < bytes.length; i++) {
25 digital = bytes[i];
26
27 if(digital < 0) {
28 digital += 256;
29 }
30 if(digital < 16){
31 md5str.append("0");
32 }
33 md5str.append(Integer.toHexString(digital));
34 }
35 return md5str.toString().toUpperCase();
36 }
37 }