import java.io.UnsupportedEncodingException;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

public class MD5 {

public static void main(String[] args) {

try {

System.out.println(getMD5EncryptedPwd("kakakakakakakakkakaklalalalala"));

} catch (Exception e) {

}

}


/**  

* 将指定byte数组转换成16进制字符串  

* @param b  

* @return  

*/

private static String byteToHexStr(byte[] b) {

StringBuffer hexString = new StringBuffer();

for (int i = 0; i < b.length; i++) {

String hex = Integer.toHexString(b[i] & 0xFF);

if (hex.length() == 1) {

hex = '0' + hex;

}

hexString.append(hex.toUpperCase());

}

return hexString.toString();

}


/**  

* 获得加密后的16进制形式字符串

* @param password  

* @return  

* @throws NoSuchAlgorithmException  

* @throws UnsupportedEncodingException  

*/

private static String getMD5EncryptedPwd(String password) throws NoSuchAlgorithmException,

UnsupportedEncodingException {

if (password == null || password.trim().length() == 0) {

return "";

}

//声明消息摘要对象   

MessageDigest md = MessageDigest.getInstance("MD5");

//将口令的数据传给消息摘要对象   

md.update(password.getBytes("UTF-8"));

//获得消息摘要的字节数组   

byte[] digest = md.digest();

//将字节数组格式加密后的口令转化为16进制字符串格式的口令   

return byteToHexStr(digest);

}

}