伊峰獨居

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            30 隨筆 :: 30 文章 :: 5 評論 :: 0 Trackbacks

          /**
          ?* Title: MD5加密算法
          ?* Description:
          ?* Copyright: Copyright (c) 2006
          ?* Company: DREAM ZONE
          ?* @author unascribed
          ?* @version 1.0
          ?*/
          import java.security.MessageDigest;

          public class StringUtil {

          ?private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",?"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

          ?/**
          ? * 轉換字節數組為16進制字串
          ? *
          ? * @param b
          ? *??????????? 字節數組
          ? * @return 16進制字串
          ? */

          ?public static String byteArrayToHexString(byte[] b) {
          ??StringBuffer resultSb = new StringBuffer();
          ??for (int i = 0; i < b.length; i++) {
          ???resultSb.append(byteToHexString(b[i]));
          ??}
          ??return resultSb.toString();
          ?}

          ?private static String byteToHexString(byte b) {
          ??int n = b;
          ??if (n < 0)
          ???n = 256 + n;
          ??int d1 = n / 16;
          ??int d2 = n % 16;
          ??return hexDigits[d1] + hexDigits[d2];
          ?}

          ?public static String MD5Encode(String origin) {
          ??String resultString = null;

          ??try {
          ???resultString = new String(origin);
          ???MessageDigest md = MessageDigest.getInstance("MD5");
          ???resultString = byteArrayToHexString(md.digest(resultString
          ?????.getBytes()));
          ??} catch (Exception ex) {

          ??}
          ??return resultString;
          ?}
          ?
          ?//測試方法
          ?public static void main(String[] args) {
          ??String res = MD5Encode("1");
          ??System.err.println(res);
          ?}
          }

          posted on 2006-08-25 13:49 伊峰 閱讀(255) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 津市市| 黄山市| 西林县| 延吉市| 镇康县| 新邵县| 桦甸市| 富民县| 海城市| 宜城市| 兰考县| 惠州市| 夏河县| 台南县| 东乌珠穆沁旗| 华阴市| 扎赉特旗| 康保县| 曲麻莱县| 多伦县| 舟曲县| 衡东县| 金山区| 巨鹿县| 温宿县| 康定县| 古交市| 民丰县| 衡阳县| 景泰县| 安义县| 九寨沟县| 安阳县| 桑日县| 阿拉善盟| 苏州市| 长泰县| 华安县| 富阳市| 昌邑市| 上林县|