md5加密算法
package com.google;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @author CQY13 MD5加密工具類
*/
public class MD5Utils {
/**
* 獲取MD5加密
*
* @param pwd
* 需要加密的字符串
* @return String字符串 加密後的字符串
*/
public static String getPwd(String pwd) {
try {
MessageDigest digest = MessageDigest.getInstance("md5");
byte[] bs = digest.digest(pwd.getBytes());
String hexString = "";
for (byte b : bs) {
int temp = b & 255;
if (temp < 16 && temp >= 0) {
// 手動補上一個“0”
hexString = hexString + "0" + Integer.toHexString(temp);
} else {
hexString = hexString + Integer.toHexString(temp);
}
}
return hexString;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
/**
* @param args
*/
public static void main(String[] args) {
String pwd = MD5Utils.getPwd("admin!@#$..");
System.out.println(pwd);
}
}
md5加密算法