首页 > 代码库 > MD5 加密 java代码实现

MD5 加密 java代码实现

 1 package com.company.fjf; 2  3 import java.security.MessageDigest; 4 import java.security.NoSuchAlgorithmException; 5  6 public class MD5Test { 7     private final static String[] strDigits = { "0", "1", "2", "3", "4", "5", 8             "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; 9 10     public MD5Test() {11     }12 13     // return Hexadecimal14     private static String byteToArrayString(byte bByte) {15         int iRet = bByte;16         if (iRet < 0) {17             iRet += 256;18         }19         int iD1 = iRet / 16;20         int iD2 = iRet % 16;21         return strDigits[iD1] + strDigits[iD2];22     }23 24     // 转换字节数组为16进制字串25     private static String byteToString(byte[] bByte) {26         StringBuffer sBuffer = new StringBuffer();27         for (int i = 0; i < bByte.length; i++) {28             sBuffer.append(byteToArrayString(bByte[i]));29         }30         return sBuffer.toString().toUpperCase();31     }32 33     public static String GetMD5Code(String strObj) {34         String resultString = null;35         try {36             resultString = new String(strObj);37             MessageDigest md = MessageDigest.getInstance("MD5");38             // md.digest() 该函数返回值为存放哈希值结果的byte数组39             resultString = byteToString(md.digest(strObj.getBytes()));40         } catch (NoSuchAlgorithmException ex) {41             ex.printStackTrace();42         }43         return resultString;44     }45 46     public static void main(String[] args) {47         System.out.println(MD5Test.GetMD5Code("aaaa"));48     }49 }

 

MD5 加密 java代码实现