Java字符串加密處理

 iLife's 博客http://blog.csdn.net/fei1502816 


/**
 * 2010-04-08
 * 爲字符串加密
 * author: 001
 *
 */
public class PwdDigest {
	
	/**
	 * @param myinfo
	 * 爲字符串加密
	 * @return
	 */
	public static String passwordDigest(String myinfo) {
		String newPwd = "";
		try {
			java.security.MessageDigest alga=java.security.MessageDigest.getInstance("MD5");
//			java.security.MessageDigest alga = java.security.MessageDigest.getInstance("SHA-1");
			alga.update(myinfo.getBytes());
			byte[] digesta = alga.digest();
			newPwd = byte2hex(digesta);
		} catch (java.security.NoSuchAlgorithmException e) {
			System.out.println("密碼加密異常:非法摘要算法"+e);
			Loggers.error("密碼加密異常:非法摘要算法"+e);
		}
		return newPwd;

	}

	public static String byte2hex(byte[] b) // 二行制轉字符串
	{
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1){
				hs = hs + "0" + stmp;
			}else{
				hs = hs + stmp;
			}
		}
		return hs.toUpperCase();
	}
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章