Java md5加密

private static final String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};


/**
* MD5加密字符串
* @param target 要加密的字符串
* @return 加密后的字符串
* */
public static String md5(String target) {

if (StringUtils.isBlank(target)) return null;

MessageDigest md = null;

try {
md = MessageDigest.getInstance("MD5");

byte[] result = md.digest(target.getBytes());

String str = byteArrayToHexString(result);

return str;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

/**
* 转换字节数组为十六进制字符串
* @param b 字节数组
* @return 十六进制字符串
* */
private static String byteArrayToHexString(byte[] b) {
StringBuffer buffer = new StringBuffer();
for (int i = 0;i < b.length;i ++ ) {
buffer.append(byteToHexString(b[i]));
}
return buffer.toString();
}

/**
* 将一个字节转化成十六进制形式的字符串
* @param b 字节
* @return 十六进制字符串
* */
private static String byteToHexString(byte b) {
int n = b;
if (n < 0) n = 256 + n;
int i = n / 16;
int j = n % 16;
return hexDigits[i] + hexDigits[j];
}
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页