MD5

9/6/2015来源:Java教程人气:2301

md5
 1 public class MD5 { 2      3     // 全局数组 4     PRivate final static String[] strDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; 6  7     // 返回形式为数字跟字符串 8     private static String byteToArrayString(byte bByte) { 9         int iRet = bByte;11         if (iRet < 0) {12             iRet += 256;13         }14         int iD1 = iRet / 16;15         int iD2 = iRet % 16;16         return strDigits[iD1] + strDigits[iD2];17     }18 19     // 转换字节数组为16进制字串20     private static String byteToString(byte[] bByte) {21         StringBuffer sBuffer = new StringBuffer();22         for (int i = 0; i < bByte.length; i++) {23             sBuffer.append(byteToArrayString(bByte[i]));24         }25         return sBuffer.toString();26     }27 28     public static String GetMD5Code(String strObj) {29         String resultString = null;30         try {31             resultString = new String(strObj);32             MessageDigest md = MessageDigest.getInstance("MD5");33             resultString = byteToString(md.digest(strObj.getBytes()));34         } catch (NoSuchAlgorithmException ex) {35             ex.printStackTrace();36         }37         return resultString;38     }39 }