近期公司需要大量接口对接,与快递有对接,与淘宝也有对接,免不了需要用到MD5加密,但MD5加密网上一大堆乱七八糟的加密方法,搞得头晕,最终,我找到了适应自己的加密方法。
我需要的效果跟这个网站https://md5jiami.51240.com/的‘32位 大写’的结果是一样的。
代码:
public final static String MD5(String s) {
char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
try {
byte[] btInput = s.getBytes("utf-8");
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(btInput);
byte[] md = mdInst.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
return "";
}
}
网友评论