通用32位MD5演算法總結:MD5Util

來源:互聯網
上載者:User

標籤:java   os   io   for   re   c   

1.小寫:

比如

abcdef
e80b5017098950fc58aad83c8c14978e


 public static String HEXAndMd5(String plainText) {    try {      MessageDigest md = MessageDigest.getInstance("MD5");      try {        md.update(plainText.getBytes("UTF8"));      } catch (UnsupportedEncodingException e) {        e.printStackTrace();      }      byte b[] = md.digest();      int i;      StringBuffer buf = new StringBuffer(200);      for (int offset = 0; offset < b.length; offset++) {        i = b[offset] & 0xff;        if (i < 16) buf.append("0");        buf.append(Integer.toHexString(i));      }      return buf.toString();    } catch (NoSuchAlgorithmException e) {      LoggerUtil.error("Md5加密異常", e);      return null;    }  }


 



1.大寫:

比如

abcdef
E80B5017098950FC58AAD83C8C14978E


   /**     * 獲得MD5加密字串     *     * @param source 源字串     *     * @return 加密後的字串     *     */    public static String getMD5(String source) {        String mdString = null;        if (source != null) {            try {//關鍵是這句                mdString = getMD5(source.getBytes("UTF-8"));            } catch (UnsupportedEncodingException e) {                e.printStackTrace();            }        }        return mdString;    }    /**     * 獲得MD5加密字串     *     * @param source 源位元組數組     *     * @return 加密後的字串     */    public static String getMD5(byte[] source) {        String s = null;        char [] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8',                '9', 'A', 'B', 'C', 'D', 'E', 'F'};        final int temp = 0xf;        final int arraySize = 32;        final int strLen = 16;        final int offset = 4;        try {            java.security.MessageDigest md = java.security.MessageDigest                    .getInstance("MD5");            md.update(source);            byte [] tmp = md.digest();            char [] str = new char[arraySize];            int k = 0;            for (int i = 0; i < strLen; i++) {                byte byte0 = tmp[i];                str[k++] = hexDigits[byte0 >>> offset & temp];                str[k++] = hexDigits[byte0 & temp];            }            s = new String(str);        } catch (Exception e) {            e.printStackTrace();        }        return s;    }










聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.