Java学习笔记25:Java中MD5使用

import java.security.MessageDigest;  
  1.   
  2.   
  3. /** 
  4.  * MD5工具类 
  5.  * @author zhuli.zhul@aliyun-inc.com 2012-12-19 下午6:22:42 
  6.  */  
  7. public class Md5Util {   
  8.       
  9.     /** 
  10.      * 字符串MD5加密后返回字符串格式 
  11.      * @param md5Str 
  12.      * @return 
  13.      */  
  14.     public static String MD5(String md5Str) {   
  15.         byte[] btInput = md5Str.getBytes();  
  16.         char hexDigits[] = { '0''1''2''3''4''5''6''7''8''9',   
  17.                              'a''b''c''d''e''f'   
  18.                            };   
  19.         try {   
  20.             MessageDigest mdInst = MessageDigest.getInstance("MD5");   
  21.             mdInst.update(btInput);   
  22.             byte[] md = mdInst.digest();   
  23.             int j = md.length;   
  24.             char str[] = new char[j * 2];   
  25.             int k = 0;   
  26.             for (int i = 0; i < j; i++)   
  27.             {   
  28.                 byte byte0 = md[i];   
  29.                 str[k++] = hexDigits[byte0 >>> 4 & 0xf];   
  30.                 str[k++] = hexDigits[byte0 & 0xf];   
  31.             }   
  32.             return new String(str);   
  33.         } catch (Exception e) {   
  34.             return null;   
  35.         }   
  36.     }   
  37. }  
import java.security.MessageDigest;


/**
 * MD5工具类
 * @author zhuli.zhul@aliyun-inc.com 2012-12-19 下午6:22:42
 */
public class Md5Util { 
    
    /**
     * 字符串MD5加密后返回字符串格式
     * @param md5Str
     * @return
     */
    public static String MD5(String md5Str) { 
        byte[] btInput = md5Str.getBytes();
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
                             'a', 'b', 'c', 'd', 'e', 'f' 
                           }; 
        try { 
            MessageDigest mdInst = MessageDigest.getInstance("MD5"); 
            mdInst.update(btInput); 
            byte[] md = mdInst.digest(); 
            int j = md.length; 
            char str[] = new char[j * 2]; 
            int k = 0; 
            for (int i = 0; i < j; i++) 
            { 
                byte byte0 = md[i]; 
                str[k++] = hexDigits[byte0 >>> 4 & 0xf]; 
                str[k++] = hexDigits[byte0 & 0xf]; 
            } 
            return new String(str); 
        } catch (Exception e) { 
            return null; 
        } 
    } 
}


一个MD5的工具类,使用包:java.security.MessageDigest; 是二进制的,所以需要进行处理之后,才能传递进去一个字符串,获得到一个字符串


备注:http://propedit.sourceforge.jp/eclipse/updates/ 国际化语言 插件


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值