1 package com.gnnuit.mobilesafe.util; 2 3 import java.security.MessageDigest; 4 import java.security.NoSuchAlgorithmException; 5 6 public class MD5Encoder { 7 /** 8 * MD5加密 9 * 10 * @param pwd要加密的数据 11 * @return 加密后的数据 12 * @throws Exception 13 */ 14 public static String encode(String pwd) { 15 try { 16 MessageDigest digest = MessageDigest.getInstance("MD5"); 17 byte[] bytes = digest.digest(pwd.getBytes()); 18 StringBuffer sb = new StringBuffer(); 19 for (int i = 0; i < bytes.length; i++) { 20 String s = Integer.toHexString(0xff & bytes[i]); 21 if (s.length() == 1) { 22 sb.append("0" + s); 23 } else { 24 sb.append(s); 25 } 26 } 27 return sb.toString(); 28 } catch (NoSuchAlgorithmException e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 throw new RuntimeException("永远不会发生"); 32 } 33 } 34 }