1 package gboat2.web.util;
2
3 import java.io.UnsupportedEncodingException;
4 import java.security.MessageDigest;
5 import java.security.NoSuchAlgorithmException;
6
7 public class EncryptUtil {
8
9
10 public static String md5(String inputText) {
11 return encrypt(inputText, "md5");
12 }
13
14
15 public static String sha(String inputText) {
16 return encrypt(inputText, "sha-1");
17 }
18
19
20
21
22
23
24
25
26
27
28 private static String encrypt(String inputText, String algorithmName) {
29 if (inputText == null || "".equals(inputText.trim())) {
30 throw new IllegalArgumentException("请输入要加密的内容");
31 }
32 if (algorithmName == null || "".equals(algorithmName.trim())) {
33 algorithmName = "md5";
34 }
35 String encryptText = null;
36 try {
37 MessageDigest m = MessageDigest.getInstance(algorithmName);
38 m.update(inputText.getBytes("UTF8"));
39 byte s[] = m.digest();
40
41 return hex(s);
42 } catch (NoSuchAlgorithmException e) {
43 e.printStackTrace();
44 } catch (UnsupportedEncodingException e) {
45 e.printStackTrace();
46 }
47 return encryptText;
48 }
49
50
51 private static String hex(byte[] arr) {
52 StringBuffer sb = new StringBuffer();
53 for (int i = 0; i < arr.length; ++i) {
54 sb.append(Integer.toHexString((arr[i] & 0xFF) | 0x100).substring(1,
55 3));
56 }
57 return sb.toString();
58 }
59
60 public static void main(String[] args) {
61
62 String md5_1 = md5("yjf_123456789.11.88");
63 System.out.println("-------->" + md5_1);
64
65
66
67
68
69
70
71
72 }
73 }