. 1 Package com.iwhalecloud.retail.enterprise.c.utils; 2 . 3 Import java.io.UnsupportedEncodingException; . 4 Import the 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 // byte array converted to a hexadecimal string . 11 Private static string byteArrayToHexString ( byte B []) { 12 is the StringBuffer resultSb =new new the StringBuffer (); 13 is for ( int I = 0; I <to b.length; I ++ ) 14 resultSb.append (byteToHexString (B [I])); 15 16 return resultSb.toString (); . 17 } 18 is . 19 // conversion of a single byte to be integers taking commercially number modulo calculation 20 is Private static String byteToHexString ( byte B) { 21 is int n-= B; 22 is IF (n-<0 ) 23 is n-+ = 256 ; 24 int D1 = n-/ 16 ; 25 int D2 = n-16% ; 26 is // go array according subscript d1, d2 hexDigits data 27 return hexdigits [D1] + hexdigits [D2]; 28 } 29 30 public static String MD5Encode (Origin String, String charsetName) { 31 is ResultString = String null ; 32 the try { 33 is ResultString = new new String (Origin); 34 is // the MessageDigest object gets MD5 digest algorithm 35 the MessageDigest MessageDigest.getInstance MD = ( "MD5" ); 36 IF(charsetName == null || "" .equals (charsetName)) 37 [ // bytes after the encrypted data is converted into a string of hexadecimal 38 is ResultString = byteArrayToHexString (md.digest (ResultString 39 .getBytes ())) ; 40 the else 41 is ResultString = byteArrayToHexString (md.digest (ResultString 42 is .getBytes (charsetName))); 43 is } the catch (Exception Exception) { 44 is } 45 return ResultString; 46 is } 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 is * MD5 encrypted string (32 uppercase) 52 is * 53 is * @param string required MD5 encrypted string 54 is * @return encrypted string (upper) 55 * / 56 is public static string md5Encrypt32Upper (string string) { 57 is byte [] the hash; 58 the try { 59 // create a MD5 algorithm object, and to obtain an array of bytes 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: the hash) { 70 IF ((B & 0xFF) <0x10) hex.append ( "0" ); 71 is hex.append (Integer.toHexString (B & 0xFF )); 72 } 73 is return hex. toString () the toUpperCase ();. 74 } 75 76 / ** 77 * MD5 encrypted string (lower case 32) 78 * 79 * @param string MD5 encryption string need 80 * @return encrypted string ( lowercase) 81 * / 82 public staticMd5Encrypt32Lower String (String String) { 83 // directly above method to convert it into a lower case 84 return md5Encrypt32Upper (String) .toLowerCase (); 85 } 86 87 / ** 88 * byte array into a binary hexadecimal string system 89 * 90 * @param bytes binary arrays 91 is * @return hexadecimal string 92 * / 93 public static string bytesToHex ( byte [] bytes) { 94 the StringBuffer hexStr = new new the 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 }