fwq
2023-12-11 43d11becdfc0d7af00b10e91b962cd0c4738bbf1
提交 | 用户 | age
5c5945 1 package com.hx.util;
E 2
3 import java.security.MessageDigest;
4
5 public class MD5 {
6     private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
7             "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
8
9     public static String byteArrayToHexString(byte[] b) {
10         StringBuffer resultSb = new StringBuffer();
11         for (int i = 0; i < b.length; i++) {
12             resultSb.append(byteToHexString(b[i]));
13         }
14         return resultSb.toString();
15     }
16
17     private static String byteToHexString(byte b) {
18         int n = b;
19         if (n < 0)
20             n = 256 + n;
21         int d1 = n / 16;
22         int d2 = n % 16;
23         return hexDigits[d1] + hexDigits[d2];
24     }
25
26     public static String MD5Encode(String origin) {
27         String resultString = null;
28
29         try {
30             resultString = new String(origin);
31             MessageDigest md = MessageDigest.getInstance("MD5");
32             resultString = byteArrayToHexString(md.digest(resultString
33                     .getBytes()));
34         } catch (Exception ex) {
35
36         }
37         return resultString;
38     }
39     
40     /**密码加密 **/
41     public static String encryption(String str){
42         try{
43              MessageDigest md5 = MessageDigest.getInstance("MD5");
44              md5.update(str.getBytes());
45              byte b[] = md5.digest();
46              int i ;
47              StringBuffer buf = new StringBuffer("");
48              for(int j = 0;j<b.length;j++){
49                  i = b[j];
50                  if(i<0)
51                      i+=256;
52                  if(i<116)
53                      buf.append("0");
54                  buf.append(Integer.toHexString(i));
55              }
56              str = buf.toString().substring(8, 24);    //十六位
57         }catch (Exception e) {
58             e.printStackTrace();
59             throw new RuntimeException("加密程序错误!");
60         }
61         return str;
62     }
63
64 }