Blame view

src/com/ectrip/cyt/utils/EncryptUtil.java 4.55 KB
07670a44   黄灿宏   畅游通核销app: 1.增加日志上...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  package com.ectrip.cyt.utils;
  
  
  import java.io.UnsupportedEncodingException;
  import java.security.InvalidKeyException;
  import java.security.MessageDigest;
  import java.security.NoSuchAlgorithmException;
  import java.security.SecureRandom;
  import java.security.spec.InvalidKeySpecException;
  
  import javax.crypto.BadPaddingException;
  import javax.crypto.Cipher;
  import javax.crypto.IllegalBlockSizeException;
  import javax.crypto.NoSuchPaddingException;
  import javax.crypto.SecretKey;
  import javax.crypto.SecretKeyFactory;
  import javax.crypto.spec.DESedeKeySpec;
  
  
  /**
   * 基础加密算法类?当前支持des,md5?
   *
   * @author huhaopeng
   */
  public class EncryptUtil {
  
      /**
       * MD5值计码?p>
       * MD5的算法在RFC1321 中定码?
       * RFC 1321中,给出了Test suite用来码?码码你的实现是否正确码?
       * MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
       * MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661
       * MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
       * MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0
       * MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
       * @param str 源字符串
       * @return md5?
       */
      public final static byte[] md5(String str) {
          try {
              byte[] res = str.getBytes("UTF-8");
              MessageDigest mdTemp = MessageDigest.getInstance("MD5".toUpperCase());
              mdTemp.update(res);
              byte[] hash = mdTemp.digest();
              return hash;
          } catch (Exception e) {
              return null;
          }
      }
  
      //hex repr. of md5
      public final static String MD5Hex(String input) {
          String s = null;
          char hexDigits[] = { // 用来将字节转换成 16 进制表示的字码?
                  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
                  'e', 'f'};
          try {
              MessageDigest md = MessageDigest
                      .getInstance("MD5");
              md.update(input.getBytes("utf-8"));
              byte tmp[] = md.digest();
  
              char str[] = new char[16 * 2];
  
              int k = 0;
              for (int i = 0; i < 16; i++) {
  
                  byte byte0 = tmp[i];
                  str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                  str[k++] = hexDigits[byte0 & 0xf];
              }
              s = new String(str);
          } catch (Exception e) {
              e.printStackTrace();
          }
          return s;
      }
  
      // 加密后解码?
      public static String JM(byte[] inStr) {
          String newStr = new String(inStr);
          char[] a = newStr.toCharArray();
          for (int i = 0; i < a.length; i++) {
              a[i] = (char) (a[i] ^ 't');
          }
          String k = new String(a);
          return k;
      }
  
  
  
  
  
  
  
      /**
       * @param key 24位密码?
       * @param str 源字符串
       * @return
       * @throws NoSuchAlgorithmException
       * @throws NoSuchPaddingException
       * @throws InvalidKeyException
       * @throws UnsupportedEncodingException
       * @throws InvalidKeySpecException
       * @throws IllegalBlockSizeException
       * @throws BadPaddingException
       */
      public static byte[] des3Encrypt(String key, String str) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException {
  
          byte[] newkey = key.getBytes();
  
          SecureRandom sr = new SecureRandom();
  
          DESedeKeySpec dks = new DESedeKeySpec(newkey);
  
          SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
  
          SecretKey securekey = keyFactory.generateSecret(dks);
  
          Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
  
          cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
  
          byte[] bt = cipher.doFinal(str.getBytes("utf-8"));
  
          return bt;
      }
  
  
      public final static String taoBaoMD5(String s) {
          char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                  'a', 'b', 'c', 'd', 'e', 'f'};
          try {
              byte[] strTemp = s.getBytes("GBK");
              MessageDigest mdTemp = MessageDigest.getInstance("MD5");
              mdTemp.update(strTemp);
              byte[] md = mdTemp.digest();
              int j = md.length;
              char str[] = new char[j * 2];
              int k = 0;
              for (int i = 0; i < j; i++) {
                  byte byte0 = md[i];
                  str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                  str[k++] = hexDigits[byte0 & 0xf];
              }
              return new String(str);
          } catch (Exception e) {
              return null;
          }
      }
  }