import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
// -------------------------------------------------------------------------------------------------------------
public class MD5Util {
//32位
public static String getMD5String(String str) {
byte[] digest;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
//32位加密
return buf.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
// 16是表示转换为16进制数
//return new BigInteger(1, digest).toString(16);
}
/**
* 32 位: 8B09BB129036101C21675BB903A06D5D
* 16 位: 9036101C21675BB9
* 16 位 md5 其实是 32 位 md5 的中间 16 个字.
*/
public static String trim32To16Upper(String md5In32) {
String upper16 = md5In32.toUpperCase();
if (upper16.length() == 32) {
upper16 = upper16.substring(8, 24);
}
return upper16;
}
}
02-01
4473
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交