Md5Code.java 952 字节
package com.espeed.tool;

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

//用md5对指定文本加密(采用的是32位加密)
public class Md5Code
{
	public static String createMd5(String txt) throws Exception
	{
		 String result = null;
		  try {
		   MessageDigest md = MessageDigest.getInstance("MD5");
		   md.update(txt.getBytes());
		   byte b[] = md.digest();
		   int i;
		   StringBuffer buf = new StringBuffer("");
		   for (int offset = 0; offset < b.length; offset++) 
		   {
		    i = b[offset];
		    if (i < 0)
		     i += 256;
		    if (i < 16)
		     buf.append("0");
		    buf.append(Integer.toHexString(i));
		   }
		    result = buf.toString();  //md5 32bit
		  } catch (NoSuchAlgorithmException e) {
		   e.printStackTrace();
		  }
		  return result;
	
	}
	
	
	//测试
	public static void main(String[] args) throws Exception
	{
		System.out.println(Md5Code.createMd5("sz123456789"));
	}
}