1、MD5加密类
import java.security.MessageDigest;
/** * 将编码格式改成 UTF-8 */
public class Md5 {
private static final String encryModel = "MD5";
/** * md5加密方法 * @param str * @return */
public static String md5(String str) throws Exception {
return md5(encryModel, str.getBytes("utf-8"));
}
public static String md5(String algorithm, byte[] data) {
try {
MessageDigest md = MessageDigest.getInstance(algorithm);
md.update(data);
StringBuffer sb = new StringBuffer();
byte[] bytes = md.digest();
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i] & 0xFF;
if (b < 0x10) {
sb.append('0');
}
sb.append(Integer.toHexString(b));
}
return sb.toString();
} catch (Exception e) {
return ""; } } }
2、调用MD5对字符串进行加密
Md5.md5("要加密的字符串");