使用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 }