MD5工具类
1 import java.security.MessageDigest; 2 3 public class MD5Tools { 4 public final static String MD5(String s) { 5 char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 6 'A', 'B', 'C', 'D', 'E', 'F' }; 7 try { 8 byte[] btInput = s.getBytes(); 9 // 获得MD5摘要算法的 MessageDigest 对象 10 MessageDigest mdInst = MessageDigest.getInstance("MD5"); 11 // 使用指定的字节更新摘要 12 mdInst.update(btInput); 13 // 获得密文 14 byte[] md = mdInst.digest(); 15 // 把密文转换成十六进制的字符串形式 16 int j = md.length; 17 char str[] = new char[j * 2]; 18 int k = 0; 19 for (int i = 0; i < j; i++) { 20 byte byte0 = md[i]; 21 str[k++] = hexDigits[byte0 >>> 4 & 0xf]; 22 str[k++] = hexDigits[byte0 & 0xf]; 23 } 24 return new String(str); 25 } catch (Exception e) { 26 e.printStackTrace(); 27 return null; 28 } 29 } 30 31 public static String getAccountMD5(UsAccount account){ 32 String sprit ="|"; 33 String key = "mooyii2015"; 34 StringBuffer sb = new StringBuffer(); 35 sb.append(account.getUserId()+sprit); 36 sb.append(account.getOrgId()+sprit); 37 sb.append(account.getAccountCash()+sprit); 38 sb.append(account.getAccountVirtual()+sprit); 39 sb.append(account.getAccountBonus()+sprit); 40 sb.append(key); 41 System.out.println(sb.toString()); 42 return MD5(sb.toString()); 43 } 44 45 public static void main(String[] args){ 46 UsAccount account = new UsAccount(); 47 account.setUserId("user201511021029361178621"); 48 account.setOrgId("user201511021029361178621"); 49 account.setAccountCash("1000"); 50 account.setAccountBonus("200"); 51 account.setAccountVirtual("42"); 52 System.out.println(MD5Tools.getAccountMD5(account)); 53 System.out.println(MD5Tools.getAccountMD5(account).length()); 54 } 55 }