Java版的加密byte数组在网上很难搜索出来。。
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Util {
private static char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
private static MessageDigest messagedigest = null;
/**
* MessageDigest初始化
*/
static {
try {
messagedigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
System.err.println("MD5FileUtil messagedigest初始化失败");
e.printStackTrace();
}
}
/**
* 对字符串进行MD5加密
*/
public static String getMD5String(String s) {
return getMD5String(s.getBytes());
}
/**
* 对byte类型的数组进行MD5加密
*/
public static String getMD5String(byte[] bytes) {
messagedigest.update(bytes);
return bufferToHex(messagedigest.digest());
}
private static String bufferToHex(byte bytes[]) {
return bufferToHex(bytes, 0, bytes.length);
}
private static String bufferToHex(byte bytes[], int m, int n) {
StringBuilder stringbuffer = new StringBuilder(2 * n);
int k = m + n;
for (int l = m; l < k; l++) {
char c0 = hexDigits[(bytes[l] & 0xf0) >> 4];
char c1 = hexDigits[bytes[l] & 0xf];
stringbuffer.append(c0);
stringbuffer.append(c1);
}
return stringbuffer.toString();
}
}