32位MD5加密Java代码

阅读更多

 

可以直接使用,直接调用getMD5Str(String str)这个方法,即可返回一个经过MD5加密的字符串。

 

MD5.java:

 

package com.icss.test;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.UnsupportedEncodingException;

public class Md5 {

	public String getMD5Str(String str) throws NoSuchAlgorithmException,
			UnsupportedEncodingException {
		MessageDigest messageDigest = null;

		messageDigest = MessageDigest.getInstance("MD5");

		messageDigest.reset();

		messageDigest.update(str.getBytes("UTF-8"));

		byte[] byteArray = messageDigest.digest();

		StringBuffer md5StrBuff = new StringBuffer();

		for (int i = 0; i < byteArray.length; i++) {
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
				md5StrBuff.append("0").append(
						Integer.toHexString(0xFF & byteArray[i]));
			else
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
		}

		return md5StrBuff.toString();
	}

}

 

你可能感兴趣的:(MD5,Java)