当前位置:开发者网络 >> 技术教程 >> JSP教程 >> Java技巧及代码 >> 内容
精彩推荐
分类最新教程
分类热点教程
  
MD5算法(看在你是女孩的份上,给你一个吧)
作者:未知
日期:2003-07-26
人气:
投稿:Andy.m(转贴)
来源:未知
字体:
收藏:加入浏览器收藏
以下正文:
  public String MD5(String sInput)
   throws Exception{
     
     String algorithm="";
     //输入不能为空
     if(sInput.trim()==null){
    return "null";
     }  
     
     //指定采用MD5算法
     try{
        algorithm=System.getProperty("MD5.algorithm","MD5");
     }catch(SecurityException se){
     }
     
     //定义MessageDigest对象
     MessageDigest md=MessageDigest.getInstance(algorithm);
     
     //按照系统缺省的字符编码方式把sInput 转换成字节,并把结果存到一新的字节数组buffer中
     byte buffer[]=sInput.getBytes();
     
     //从指定的字节数组buffer的偏移量0开始,用指定的字节数组修改由sInput生成摘要
     //count为从 0 开始用的字节数长度。    
     for(int count=0;count<sInput.length();count++)
     {
    md.update(buffer,0,count);
     }
     
     //通过执行最后的诸如填充的操作完成散列码的计算。 在调用之后复位该摘要
     //返回存放结果散列值的字节数组bDigest
     byte bDigest[]=md.digest();
     
     //将bDigest转换为大整数bi
     BigInteger bi=new BigInteger(bDigest);
     
     //返回bi字符串表示,即最终的编码结果
     return(bi.toString(16));  
   }
 
相关文章: