java教程

java Md5util加密函数

位置:首页 > java教程 > java技巧,2019-08-28 22:22
java Md5util加密函数
public class Md5util {

	private static StringBuffer stringBuffer;

	public static String encoder(String psd){
		psd = "mutisafe" + psd + "@leafotto.tk";
		try {
			//指定加密算法
			MessageDigest digest = MessageDigest.getInstance("MD5");
			//将需要加密的字符串转换成byte类型的数组,然后进行随机哈希
			byte[] bs = digest.digest(psd.getBytes());
			//循环遍历bs,然后让其生成32位字符串,固定写法
			stringBuffer = new StringBuffer();
			for(byte b : bs){
				int i = b & 0xff;
				String hexString = Integer.toHexString(i);
				if(hexString.length() < 2){
					hexString = "0" + hexString;
				}
				//拼接字符串
				stringBuffer.append(hexString);
			}
			return stringBuffer.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return "";
	}
}

TAGS:java Md5加密

猜你喜欢

NewHot