现在的位置: 首页 > 综合 > 正文

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

2013年11月12日 ⁄ 综合 ⁄ 共 3681字 ⁄ 字号 评论关闭
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. }  

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

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

  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. }  

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

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

抱歉!评论已关闭.