Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26277948
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Java

2010-03-23 20:14:43

package cn.ty;
import java.security.*;
import java.util.*;
public class Md5 {
    private String inStr;
    private MessageDigest md5;

    /**
     * Constructs the MD5 object and sets the string whose MD5 is to be computed.
     *
     * @param inStr the String whose MD5 is to be computed
     */
    public Md5(String inStr)
    {
        this.inStr = inStr;
        try
        {
            this.md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e){
            System.out.println(e.toString());
            e.printStackTrace();
        }
    }

    /**
     * Computes the MD5 fingerprint of a string.
     *
     * @return the MD5 digest of the input String
     */
    public String compute()
    {
        char[] charArray = this.inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];
        for (int i=0; i            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = this.md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i=0; i           int val = ((int) md5Bytes[i] ) & 0xff;
           if (val < 16) hexValue.append("0");
           hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
     }
    
    public static void main(String[] args)
    {
        Md5 md5=new Md5("admin");
        String postString = md5.compute();
        System.out.println(postString);
    }
}
测试效果与PHP里面的md5函数效果一样!

结论:以后使用此类进行JAVA中的MD5加密输出!
 
阅读(656) | 评论(1) | 转发(0) |
0

上一篇:HTTP请求方法整理

下一篇:Query Cache

给主人留下些什么吧!~~

chinaunix网友2010-03-25 21:15:54

不要想太多。想太多容易 让人很烦燥而且坐立不安 我觉得关键就是要动手去做。把目标写出来然后按照目标行事就行! 目标是灯有灯就有光明