使用MD5對字符串加密


  1 package com.iwhalecloud.retail.enterprise.c.utils;
  2 
  3 import java.io.UnsupportedEncodingException;
  4 import java.security.MessageDigest;
  5 import java.security.NoSuchAlgorithmException;
  6 import java.util.regex.Matcher;
  7 import java.util.regex.Pattern;
  8 
  9 public class MD5Util {
 10     //將字節數組轉成十六進制字符串
 11     private static String byteArrayToHexString(byte b[]) {
 12         StringBuffer resultSb = new StringBuffer();
 13         for (int i = 0; i < b.length; i++)
 14             resultSb.append(byteToHexString(b[i]));
 15 
 16         return resultSb.toString();
 17     }
 18 
 19     //對單個字節轉換成整數進行取商取余數計算
 20     private static String byteToHexString(byte b) {
 21         int n = b;
 22         if (n < 0)
 23             n += 256;
 24         int d1 = n / 16;
 25         int d2 = n % 16;
 26         //根據下標d1,d2去數組hexDigits的數據
 27         return hexDigits[d1] + hexDigits[d2];
 28     }
 29 
 30     public static String MD5Encode(String origin, String charsetname) {
 31         String resultString = null;
 32         try {
 33             resultString = new String(origin);
 34             // 獲得MD5摘要算法的 MessageDigest 對象
 35             MessageDigest md = MessageDigest.getInstance("MD5");
 36             if (charsetname == null || "".equals(charsetname))
 37                 //將加密之后的字節數據轉換成16進制的字符串
 38                 resultString = byteArrayToHexString(md.digest(resultString
 39                         .getBytes()));
 40             else
 41                 resultString = byteArrayToHexString(md.digest(resultString
 42                         .getBytes(charsetname)));
 43         } catch (Exception exception) {
 44         }
 45         return resultString;
 46     }
 47 
 48     private static final String hexDigits[] = {"0", "1", "2", "3", "4", "5",
 49             "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
 50     /**
 51      * MD5加密字符串(32位大寫)
 52      *
 53      * @param string 需要進行MD5加密的字符串
 54      * @return 加密后的字符串(大寫)
 55      */
 56     public static String md5Encrypt32Upper(String string) {
 57         byte[] hash;
 58         try {
 59             //創建一個MD5算法對象,並獲得MD5字節數組,16*8=128位
 60             hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
 61         } catch (NoSuchAlgorithmException e) {
 62             throw new RuntimeException("Huh, MD5 should be supported?", e);
 63         } catch (UnsupportedEncodingException e) {
 64             throw new RuntimeException("Huh, UTF-8 should be supported?", e);
 65         }
 66 
 67         //轉換為十六進制字符串
 68         StringBuilder hex = new StringBuilder(hash.length * 2);
 69         for (byte b : hash) {
 70             if ((b & 0xFF) < 0x10) hex.append("0");
 71             hex.append(Integer.toHexString(b & 0xFF));
 72         }
 73         return hex.toString().toUpperCase();
 74     }
 75 
 76     /**
 77      * MD5加密字符串(32位小寫)
 78      *
 79      * @param string 需要進行MD5加密的字符串
 80      * @return 加密后的字符串(小寫)
 81      */
 82     public static String md5Encrypt32Lower(String string) {
 83         //直接上面的方法轉換成小寫就可以了
 84         return md5Encrypt32Upper(string).toLowerCase();
 85     }
 86 
 87     /**
 88      * 將二進制字節數組轉換為十六進制字符串
 89      *
 90      * @param bytes 二進制字節數組
 91      * @return 十六進制字符串
 92      */
 93     public static String bytesToHex(byte[] bytes) {
 94         StringBuffer hexStr = new StringBuffer();
 95         int num;
 96         for (int i = 0; i < bytes.length; i++) {
 97             num = bytes[i];
 98             if (num < 0) {
 99                 num += 256;
100             }
101             if (num < 16) {
102                 hexStr.append("0");
103             }
104             hexStr.append(Integer.toHexString(num));
105         }
106         return hexStr.toString().toUpperCase();
107     }
108 
109     /**
110      * Unicode中文編碼轉換成字符串
111      */
112     public static String unicodeToString(String str) {
113         Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
114         Matcher matcher = pattern.matcher(str);
115         char ch;
116         while (matcher.find()) {
117             ch = (char) Integer.parseInt(matcher.group(2), 16);
118             str = str.replace(matcher.group(1), ch + "");
119         }
120         return str;
121     }
122 
123     public static void main(String[] args) {
124         System.out.println(md5Encrypt32Lower("123456"));
125         System.out.println(md5Encrypt32Upper("123456"));
126     }
127 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM