MD5实现加密 java

package util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import sun.misc.BASE64Encoder;

public class MD5Util {
	public static void test1(){
		try {
			MessageDigest md = 
				MessageDigest.getInstance("md5");
			String str = "ihateyouasdfasdfasdf";
			byte[] buf = md.digest(str.getBytes());
			BASE64Encoder encoder = 
				new BASE64Encoder();
			String str2 = encoder.encode(buf);
			System.out.println("str2:" + str2 + " " + str2.length());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}
	
	public static String getMD5Str(String origStr) throws Exception{
			MessageDigest md = 
				MessageDigest.getInstance("md5");
			byte[] buf = md.digest(origStr.getBytes());
			BASE64Encoder encoder = 
				new BASE64Encoder();
			String str2 = encoder.encode(buf);
			return str2;
		
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		test1();
	}

}

你可能感兴趣的:(java)