md5工具類

  1. import java.security.MessageDigest;   
  2.    
  3. /**  
  4.  * MD5工具類,用於生產字符串的MD5碼  
  5.  * @author iStar  
  6.  *  
  7.  */   
  8. public class MD5 {   
  9.    
  10.     public static String getMD5(String s) {   
  11.         try {   
  12.             MessageDigest md5 = MessageDigest.getInstance("MD5");   
  13.    
  14.             byte[] byteArray = s.getBytes("ISO-8859-1");   
  15.             byte[] md5Bytes = md5.digest(byteArray);   
  16.    
  17.             StringBuffer hexValue = new StringBuffer();   
  18.    
  19.             for (int i = 0; i < md5Bytes.length; i++) {   
  20.                 int val = ((int) md5Bytes[i]) & 0xff;   
  21.                 if (val < 16)   
  22.                     hexValue.append("0");   
  23.                 hexValue.append(Integer.toHexString(val));   
  24.             }   
  25.    
  26.             return hexValue.toString();   
  27.    
  28.         } catch (Exception e) {   
  29.             e.printStackTrace();   
  30.             return null;   
  31.         }   
  32.     }   
  33. }   

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章