MD5编码

 1 import java.io.IOException;

 2 import java.security.MessageDigest;

 3 

 4 public class MD5Digestor {

 5 

 6     public static String getMd5(String content) {

 7         try {

 8             MessageDigest md = MessageDigest. getInstance("MD5");

 9             byte[] results = md.digest(content.getBytes());

10             return byteArrayToHexString(results);

11         } catch (Exception e) {

12             e.printStackTrace();

13         }

14         return null;

15     }

16 

17     /**

18      * 轮换字节数组为十六进制字符串

19      * @param b 字节数组

20      * @return 十六进制字符串

21      */

22     private static String byteArrayToHexString( byte[] b) {

23         StringBuffer resultSb = new StringBuffer();

24         for (int i = 0; i < b. length; i++) {

25             resultSb.append( byteToHexString(b[i]));

26         }

27         return resultSb.toString();

28     }

29 

30     /**

31      * 将一个字节转化成十六进制形式的字符串

32      */

33     private static String byteToHexString( byte b) {

34      System.out.println(b);

35         int n = b;

36         if (n < 0) {

37             n = 256 + n;

38         }

39         int d1 = n / 16;

40         int d2 = n % 16;

41         return HEXDIGITS[d1] + HEXDIGITS[d2];

42     }

43 

44     private static final String[] HEXDIGITS = { "0", "1", "2", "3", "4" , "5" , "6" , "7" , "8" , "9" ,

45             "A", "B", "C", "D", "E", "F" };

46 

47     public static void main (String[] args) throws IOException {

48         String mKey = "DcdOplConfig.getInstance().getClientVersion()"

49                 + "DcdOplConfig.getInstance().getPassword()" ;

50         System. out.println( getMd5(mKey));

51     }

52 

53 }

你可能感兴趣的:(MD5)