Java进行MD5加密

直接上代码,JDK6已经有一个方便点的类帮助简化步骤:

package com.cssor.test;

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

public class MD5 {
	/** 
     * MD5 加密 
     */  
    private static String getMD5Str(String str) {  
        MessageDigest messageDigest = null;  

        try {  
            messageDigest = MessageDigest.getInstance("MD5");  

            messageDigest.reset();  

            messageDigest.update(str.getBytes("UTF-8"));  
        } catch (NoSuchAlgorithmException e) {  
            System.out.println("NoSuchAlgorithmException caught!");  
            System.exit(-1);  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  

        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();  
    }  

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.err.println(MD5.getMD5Str("a"));
	}

}

来源: http://warren.iteye.com/blog/107386

http://cssor.com/java-md5.html

参与评论

电子邮件地址不会被公开。 必填项已用*标注

*

全部分类
Books(4)code(7)database(6)html&css(24)java(11)JavaScript(48)jQuery(24)linux(20)python(1)React(1)share(1)soft(4)solution(53)thinking(17)vim(9)WordPress(8)前端优化(12)拓展(33)服务器(31)移动开发(4)自然(22)