Java Blog for Alex Wan

          Let life be beautiful like summer flowers and death like autumn leaves.

          統計

          留言簿(10)

          BlogJava

          Blogs

          DIV+CSS

          JQuery相關

          友情鏈接

          常去的地方

          數據供應

          閱讀排行榜

          評論排行榜

          簡單實現MD5加密字符串

           1package util;
           2
           3import java.security.MessageDigest;
           4
           5public class StringUtil {
           6        
           7    private final static String[] hexDigits = 
           8          "0""1""2""3""4""5""6""7"
           9          "8""9""a""b""c""d""e""f"}

          10
          11      /** 
          12       * 轉換字節數組為16進制字串 
          13       * @param b 字節數組 
          14       * @return 16進制字串 
          15       */
           
          16
          17      public static String byteArrayToHexString(byte[] b) 
          18        StringBuffer resultSb = new StringBuffer(); 
          19        for (int i = 0; i < b.length; i++
          20          resultSb.append(byteToHexString(b[i])); 
          21        }
           
          22        return resultSb.toString(); 
          23      }
           
          24
          25      private static String byteToHexString(byte b) 
          26        int n = b; 
          27        if (n < 0
          28          n = 256 + n; 
          29        int d1 = n / 16
          30        int d2 = n % 16
          31        return hexDigits[d1] + hexDigits[d2]; 
          32      }
           
          33
          34      public static String MD5Encode(String origin) 
          35        String resultString = null
          36
          37        try 
          38          resultString=new String(origin); 
          39          MessageDigest md = MessageDigest.getInstance("MD5"); 
          40          resultString=byteArrayToHexString(md.digest(resultString.getBytes())); 
          41        }
           
          42        catch (Exception ex) 
          43
          44        }
           
          45        return resultString; 
          46      }
           
          47
          48      public static void main(String[] args)
          49          System.err.println(MD5Encode("")); 
          50          System.err.println(MD5Encode("a")); 
          51          System.err.println(MD5Encode("abc")); 
          52          System.err.println(MD5Encode("message digest")); 
          53          System.err.println(MD5Encode("abcdefghijklmnopqrstuvwxyz")); 
          54        
          55      }
           
          56      
          57      //MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
          58      //MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661
          59      //MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
          60      //MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0
          61      //MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
          62       
          63
          64
          65}

          66


          Let life be beautiful like summer flowers and death like autumn leaves.

          posted on 2008-06-30 23:34 Alexwan 閱讀(2114) 評論(0)  編輯  收藏 所屬分類: J2EE小筆記

          主站蜘蛛池模板: 类乌齐县| 叶城县| 咸宁市| 饶阳县| 和顺县| 江门市| 体育| 铜鼓县| 翁牛特旗| 合肥市| 通许县| 娱乐| 阿荣旗| 阿巴嘎旗| 无为县| 乌兰浩特市| 明光市| 永泰县| 天津市| 卢龙县| 大方县| 昭觉县| 衡阳县| 苍南县| 宁武县| 闻喜县| 安龙县| 辽源市| 道孚县| 沁水县| 德令哈市| 察雅县| 克山县| 清镇市| 大洼县| 隆安县| 哈巴河县| 赣州市| 工布江达县| 武川县| 怀安县|