代碼如下:
package com.suirui.meetingcontrol.service;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 1.MD5加密字符串(32位大寫(xiě))
* 2.MD5加密字符串(32位小寫(xiě))
* <p>
* MD5在線加密:https://md5jiami.51240.com/
* 3.將二進(jìn)制字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符串
* 4.Unicode中文編碼轉(zhuǎn)換成字符串
*/
public class MD5Util {
/**
* MD5加密字符串(32位大寫(xiě))
*
* @param string 需要進(jìn)行MD5加密的字符串
* @return 加密后的字符串(大寫(xiě))
*/
public static String md5Encrypt32Upper(String string) {
byte[] hash;
try {
//創(chuàng)建一個(gè)MD5算法對(duì)象,并獲得MD5字節(jié)數(shù)組,16*8=128位
hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Huh, MD5 should be supported?", e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Huh, UTF-8 should be supported?", e);
}
//轉(zhuǎn)換為十六進(jìn)制字符串
StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xFF) < 0x10) hex.append("0");
hex.append(Integer.toHexString(b & 0xFF));
}
return hex.toString().toUpperCase();
}
/**
* MD5加密字符串(32位小寫(xiě))
*
* @param string 需要進(jìn)行MD5加密的字符串
* @return 加密后的字符串(小寫(xiě))
*/
public static String md5Encrypt32Lower(String string) {
byte[] hash;
try {
//創(chuàng)建一個(gè)MD5算法對(duì)象套蒂,并獲得MD5字節(jié)數(shù)組,16*8=128位
hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Huh, MD5 should be supported?", e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Huh, UTF-8 should be supported?", e);
}
//轉(zhuǎn)換為十六進(jìn)制字符串
StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xFF) < 0x10) hex.append("0");
hex.append(Integer.toHexString(b & 0xFF));
}
return hex.toString().toLowerCase();
}
/**
* 將二進(jìn)制字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符串
*
* @param bytes 二進(jìn)制字節(jié)數(shù)組
* @return 十六進(jìn)制字符串
*/
public static String bytesToHex(byte[] bytes) {
StringBuffer hexStr = new StringBuffer();
int num;
for (int i = 0; i < bytes.length; i++) {
num = bytes[i];
if (num < 0) {
num += 256;
}
if (num < 16) {
hexStr.append("0");
}
hexStr.append(Integer.toHexString(num));
}
return hexStr.toString().toUpperCase();
}
/**
* Unicode中文編碼轉(zhuǎn)換成字符串
*/
public static String unicodeToString(String str) {
Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
Matcher matcher = pattern.matcher(str);
char ch;
while (matcher.find()) {
ch = (char) Integer.parseInt(matcher.group(2), 16);
str = str.replace(matcher.group(1), ch + "");
}
return str;
}
}
在項(xiàng)目中的應(yīng)用:
String md5Encrypt32Lower = MD5Util.md5Encrypt32Lower("0123456789");
String md5Encrypt32Upper = MD5Util.md5Encrypt32Upper("0123456789");
LogUtil.e("MD5", "大寫(xiě)=" + md5Encrypt32Upper + "钞支,小寫(xiě)=" + md5Encrypt32Lower);
早計(jì)劃茫蛹,早準(zhǔn)備,早完成烁挟。 歡迎關(guān)注婴洼!交流!Star撼嗓!
GitHub:https://github.com/wangyang0313
微信公眾號(hào):一個(gè)靈活的胖子MrWang
CSDN:https://blog.csdn.net/qq941263013