MD5加密类(可以直接用)

package md5;

import java.security.MessageDigest;

public class Sercurity {
	public Sercurity () {
	}
	/**
	 * encryption method
	 * @param strSrc
	 * @return
	 */
	public static String Encrypt(String strSrc) {
		MessageDigest md = null;
		Sercurity sercurity = new Sercurity();
		String strDes = null;
		byte b[] = strSrc.getBytes();
		try {
			md = MessageDigest.getInstance("MD5");
			md.update(b);
			strDes = sercurity.byte2Hex(md.digest());
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
		return strDes;
	}
	/**
	 * byte to hex
	 * @param bts
	 * @return
	 */
	public String byte2Hex(byte bts[]) {
		String des = "";
		String tmp = null;
		for(int i=0;i<bts.length;i++) {
			tmp = Integer.toHexString(bts[i] & 0xff);
			if (tmp.length()==1) 
				des = (new StringBuffer()).append(des).append("0").toString();
			des = (new StringBuffer()).append(des).append(tmp).toString();
		}
		return des;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.err.println(Encrypt("abcdefghijklmnopqrstuvwxyz"));
	}
}

可以用RFC 1321中给的Test suite校验一下

你可能感兴趣的:(java,Security)