欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MD5实现加密 java

程序员文章站 2024-03-20 13:21:46
...
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();
	}

}