public class MD5Utils {
/**
* 加密
*
* @param context
*/
public static String encrypByMd5(String context) {
try {
MessageDigest md = MessageDigest.getInstance("MD5")
md.update(context.getBytes())
byte[] encryContext = md.digest()
int i
StringBuffer buf = new StringBuffer("")
for (int offset = 0
i = encryContext[offset]
if (i < 0) i += 256
if (i < 16) buf.append("0")
buf.append(Integer.toHexString(i))
}
return buf.toString()
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace()
return null
}
}
public static void main(String[] args) {
System.out.println(encrypByMd5("123456"))
//e10adc3949ba59abbe56e057f20f883e
// 加盐,迭代
System.out.println(encrypByMd5("haha" + "123456"))
//迭代+把加密的结果再加密
String str = encrypByMd5("haha" + "123456")
for (int i = 0
str = encrypByMd5(str)
}
System.out.println(str)
}
}