欢迎来到林大大得csdn
今天给大家带来最全面得md5Utils,该工具类包括了三种不同得方法,可以根据字符码得类型进行加密。类型包括:GB2312,GBK,UTF-8。网上相应得资源是很少得,大家珍惜啊。
下面是工具类:
/**
* Md5加密工具类
*/
public class Md5Utils {
private Md5Utils() {
}
public static String getVal(String plainText) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
try {
md.update(plainText.getBytes("GB2312"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = ; offset < b.length; offset++) {
i = b[offset];
if (i < )
i += ;
if (i < )
buf.append("0");
buf.append(Integer.toHexString(i));
}
return buf.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
public static String getVal_GBK(String plainText) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes("GBK"));
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = ; offset < b.length; offset++) {
i = b[offset];
if (i < )
i += ;
if (i < )
buf.append("0");
buf.append(Integer.toHexString(i));
}
return buf.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
public static String getVal_UTF8(String plainText) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes("UTF-8"));
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = ; offset < b.length; offset++) {
i = b[offset];
if (i < )
i += ;
if (i < )
buf.append("0");
buf.append(Integer.toHexString(i));
}
return buf.toString().toUpperCase();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
}
==本文为原创,翻版必究==