亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java實(shí)現(xiàn)MD5加密方法匯總

 更新時(shí)間:2015年10月29日 11:44:18   投稿:hebedich  
本文給大家匯總介紹了2種java實(shí)現(xiàn)MD5加密的方法,非常的實(shí)用,這里分享給大家,學(xué)習(xí)下其中的思路,對(duì)大家學(xué)習(xí)java非常有幫助。

代碼一:

/**
 * 實(shí)現(xiàn)MD5加密
 *
 */
public class MD5 {
   
  /**
   * 獲取加密后的字符串
   * @param input
   * @return
   */
  public static String stringMD5(String pw) {
    try { 
        
       // 拿到一個(gè)MD5轉(zhuǎn)換器(如果想要SHA1參數(shù)換成”SHA1”) 
       MessageDigest messageDigest =MessageDigest.getInstance("MD5"); 
       // 輸入的字符串轉(zhuǎn)換成字節(jié)數(shù)組 
       byte[] inputByteArray = pw.getBytes(); 
       // inputByteArray是輸入字符串轉(zhuǎn)換得到的字節(jié)數(shù)組 
       messageDigest.update(inputByteArray); 
       // 轉(zhuǎn)換并返回結(jié)果,也是字節(jié)數(shù)組,包含16個(gè)元素 
       byte[] resultByteArray = messageDigest.digest(); 
       // 字符數(shù)組轉(zhuǎn)換成字符串返回 
       return byteArrayToHex(resultByteArray); 
      } catch (NoSuchAlgorithmException e) { 
       return null; 
      } 
  }
   
  public static String byteArrayToHex(byte[] byteArray) { 
     
    // 首先初始化一個(gè)字符數(shù)組,用來(lái)存放每個(gè)16進(jìn)制字符 
    char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F' }; 
    // new一個(gè)字符數(shù)組,這個(gè)就是用來(lái)組成結(jié)果字符串的(解釋一下:一個(gè)byte是八位二進(jìn)制,也就是2位十六進(jìn)制字符(2的8次方等于16的2次方)) 
    char[] resultCharArray =new char[byteArray.length * 2]; 
    // 遍歷字節(jié)數(shù)組,通過(guò)位運(yùn)算(位運(yùn)算效率高),轉(zhuǎn)換成字符放到字符數(shù)組中去 
    int index = 0; 
    for (byte b : byteArray) { 
      resultCharArray[index++] = hexDigits[b>>> 4 & 0xf]; 
      resultCharArray[index++] = hexDigits[b& 0xf]; 
    }
    // 字符數(shù)組組合成字符串返回 
    return new String(resultCharArray); 
  }
}

方法二:

package other;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/*
 * MD5 算法
*/
public class MD5 {
  
  // 全局?jǐn)?shù)組
  private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
      "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

  public MD5() {
  }

  // 返回形式為數(shù)字跟字符串
  private static String byteToArrayString(byte bByte) {
    int iRet = bByte;
    // System.out.println("iRet="+iRet);
    if (iRet < 0) {
      iRet += 256;
    }
    int iD1 = iRet / 16;
    int iD2 = iRet % 16;
    return strDigits[iD1] + strDigits[iD2];
  }

  // 返回形式只為數(shù)字
  private static String byteToNum(byte bByte) {
    int iRet = bByte;
    System.out.println("iRet1=" + iRet);
    if (iRet < 0) {
      iRet += 256;
    }
    return String.valueOf(iRet);
  }

  // 轉(zhuǎn)換字節(jié)數(shù)組為16進(jìn)制字串
  private static String byteToString(byte[] bByte) {
    StringBuffer sBuffer = new StringBuffer();
    for (int i = 0; i < bByte.length; i++) {
      sBuffer.append(byteToArrayString(bByte[i]));
    }
    return sBuffer.toString();
  }

  public static String GetMD5Code(String strObj) {
    String resultString = null;
    try {
      resultString = new String(strObj);
      MessageDigest md = MessageDigest.getInstance("MD5");
      // md.digest() 該函數(shù)返回值為存放哈希值結(jié)果的byte數(shù)組
      resultString = byteToString(md.digest(strObj.getBytes()));
    } catch (NoSuchAlgorithmException ex) {
      ex.printStackTrace();
    }
    return resultString;
  }

  public static void main(String[] args) {
    MD5 getMD5 = new MD5();
    System.out.println(getMD5.GetMD5Code("000000"));
  }
}

相關(guān)文章

最新評(píng)論