Showing
2 changed files
with
202 additions
and
2 deletions
src/main/java/com/utils/DESTools.java
0 → 100644
1 | +package com.utils; | ||
2 | + | ||
3 | +import javax.crypto.Cipher; | ||
4 | +import javax.crypto.SecretKey; | ||
5 | +import javax.crypto.SecretKeyFactory; | ||
6 | +import javax.crypto.spec.DESKeySpec; | ||
7 | +import java.security.SecureRandom; | ||
8 | +import java.util.Random; | ||
9 | + | ||
10 | +/** | ||
11 | + * DES加密解密类. | ||
12 | + */ | ||
13 | +public class DESTools { | ||
14 | + /** | ||
15 | + * 加密、解密key,用作生成二维码之用,无其他用途。 | ||
16 | + */ | ||
17 | + private static final String PASSWORD_CRYPT_KEY = "HhiTuqm7lsMVHYD4Bkj2B5jSUh0IKPNk"; | ||
18 | + | ||
19 | + private final static String ALGORITHM = "DES"; | ||
20 | + | ||
21 | + public static void main(String[] args) throws Exception { | ||
22 | + String orgStr = "202cb962ac59075b964b07152d234b70"; | ||
23 | + long start = System.currentTimeMillis(); | ||
24 | + for (int i = 0; i < 1000; i++) { | ||
25 | + orgStr = DESTools.getRandomString(20); | ||
26 | + String encrptStr = DESTools.encrypt(orgStr); | ||
27 | + String decrptStr = DESTools.decrypt(encrptStr); | ||
28 | + if (!orgStr.equals(decrptStr)) { | ||
29 | + System.out.println("error!!"); | ||
30 | + } | ||
31 | + } | ||
32 | + long end = System.currentTimeMillis(); | ||
33 | + System.out.println("time:" + (end - start)); | ||
34 | + | ||
35 | + } | ||
36 | + | ||
37 | + /** | ||
38 | + * 生成随机字符串 | ||
39 | + * | ||
40 | + * @param length | ||
41 | + * @return | ||
42 | + */ | ||
43 | + public static String getRandomString(int length) { //length表示生成字符串的长度 | ||
44 | + String base = "abcdefghijklmnopqrstuvwxyz0123456789"; | ||
45 | + Random random = new Random(); | ||
46 | + StringBuffer sb = new StringBuffer(); | ||
47 | + for (int i = 0; i < length; i++) { | ||
48 | + int number = random.nextInt(base.length()); | ||
49 | + sb.append(base.charAt(number)); | ||
50 | + } | ||
51 | + return sb.toString(); | ||
52 | + } | ||
53 | + | ||
54 | + /** | ||
55 | + * 对数据进行DES加密. | ||
56 | + * | ||
57 | + * @param data 待进行DES加密的数据 | ||
58 | + * @return 返回经过DES加密后的数据 | ||
59 | + * @throws Exception | ||
60 | + * @author <a href="mailto:xiexingxing1121@126.com" mce_href="mailto:xiexingxing1121@126.com">AmigoXie</a> | ||
61 | + * Creation date: 2007-7-31 - 下午12:06:24 | ||
62 | + */ | ||
63 | + public final static String decrypt(String data) throws Exception { | ||
64 | + return new String(decrypt(hex2byte(data.getBytes()), | ||
65 | + PASSWORD_CRYPT_KEY.getBytes())); | ||
66 | + } | ||
67 | + | ||
68 | + /** | ||
69 | + * 对数据进行DES加密. | ||
70 | + * | ||
71 | + * @param data 待进行DES加密的数据 | ||
72 | + * @return 返回经过DES加密后的数据 | ||
73 | + * @throws Exception | ||
74 | + * @author <a href="mailto:xiexingxing1121@126.com" mce_href="mailto:xiexingxing1121@126.com">AmigoXie</a> | ||
75 | + * Creation date: 2007-7-31 - 下午12:06:24 | ||
76 | + */ | ||
77 | + public final static String decrypt(String data, String key) throws Exception { | ||
78 | + return new String(decrypt(hex2byte(data.getBytes()), | ||
79 | + key.getBytes())); | ||
80 | + } | ||
81 | + | ||
82 | + /** | ||
83 | + * 对用DES加密过的数据进行解密. | ||
84 | + * | ||
85 | + * @param data DES加密数据 | ||
86 | + * @return 返回解密后的数据 | ||
87 | + * @throws Exception | ||
88 | + * @author <a href="mailto:xiexingxing1121@126.com" mce_href="mailto:xiexingxing1121@126.com">AmigoXie</a> | ||
89 | + * Creation date: 2007-7-31 - 下午12:07:54 | ||
90 | + */ | ||
91 | + public final static String encrypt(String data) throws Exception { | ||
92 | + return byte2hex(encrypt(data.getBytes(), PASSWORD_CRYPT_KEY | ||
93 | + .getBytes())); | ||
94 | + } | ||
95 | + | ||
96 | + | ||
97 | + /** | ||
98 | + * 对用DES加密过的数据进行解密. | ||
99 | + * | ||
100 | + * @param data DES加密数据 | ||
101 | + * @return 返回解密后的数据 | ||
102 | + * @throws Exception | ||
103 | + * @author <a href="mailto:xiexingxing1121@126.com" mce_href="mailto:xiexingxing1121@126.com">AmigoXie</a> | ||
104 | + * Creation date: 2007-7-31 - 下午12:07:54 | ||
105 | + */ | ||
106 | + public final static String encrypt(String data, String key) throws Exception { | ||
107 | + return byte2hex(encrypt(data.getBytes(), key | ||
108 | + .getBytes())); | ||
109 | + } | ||
110 | + | ||
111 | + | ||
112 | + /** | ||
113 | + * 用指定的key对数据进行DES加密. | ||
114 | + * | ||
115 | + * @param data 待加密的数据 | ||
116 | + * @param key DES加密的key | ||
117 | + * @return 返回DES加密后的数据 | ||
118 | + * @throws Exception | ||
119 | + * @author <a href="mailto:xiexingxing1121@126.com" mce_href="mailto:xiexingxing1121@126.com">AmigoXie</a> | ||
120 | + * Creation date: 2007-7-31 - 下午12:09:03 | ||
121 | + */ | ||
122 | + private static byte[] encrypt(byte[] data, byte[] key) throws Exception { | ||
123 | + // DES算法要求有一个可信任的随机数源 | ||
124 | + SecureRandom sr = new SecureRandom(); | ||
125 | + // 从原始密匙数据创建DESKeySpec对象 | ||
126 | + DESKeySpec dks = new DESKeySpec(key); | ||
127 | + // 创建一个密匙工厂,然后用它把DESKeySpec转换成 | ||
128 | + // 一个SecretKey对象 | ||
129 | + SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); | ||
130 | + SecretKey securekey = keyFactory.generateSecret(dks); | ||
131 | + // Cipher对象实际完成加密操作 | ||
132 | + Cipher cipher = Cipher.getInstance(ALGORITHM); | ||
133 | + // 用密匙初始化Cipher对象 | ||
134 | + cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); | ||
135 | + // 现在,获取数据并加密 | ||
136 | + // 正式执行加密操作 | ||
137 | + return cipher.doFinal(data); | ||
138 | + } | ||
139 | + | ||
140 | + /** | ||
141 | + * 用指定的key对数据进行DES解密. | ||
142 | + * | ||
143 | + * @param data 待解密的数据 | ||
144 | + * @param key DES解密的key | ||
145 | + * @return 返回DES解密后的数据 | ||
146 | + * @throws Exception | ||
147 | + * @author <a href="mailto:xiexingxing1121@126.com" mce_href="mailto:xiexingxing1121@126.com">AmigoXie</a> | ||
148 | + * Creation date: 2007-7-31 - 下午12:10:34 | ||
149 | + */ | ||
150 | + private static byte[] decrypt(byte[] data, byte[] key) throws Exception { | ||
151 | + // DES算法要求有一个可信任的随机数源 | ||
152 | + SecureRandom sr = new SecureRandom(); | ||
153 | + // 从原始密匙数据创建一个DESKeySpec对象 | ||
154 | + DESKeySpec dks = new DESKeySpec(key); | ||
155 | + // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成 | ||
156 | + // 一个SecretKey对象 | ||
157 | + SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); | ||
158 | + SecretKey securekey = keyFactory.generateSecret(dks); | ||
159 | + // Cipher对象实际完成解密操作 | ||
160 | + Cipher cipher = Cipher.getInstance(ALGORITHM); | ||
161 | + // 用密匙初始化Cipher对象 | ||
162 | + cipher.init(Cipher.DECRYPT_MODE, securekey, sr); | ||
163 | + // 现在,获取数据并解密 | ||
164 | + // 正式执行解密操作 | ||
165 | + return cipher.doFinal(data); | ||
166 | + } | ||
167 | + | ||
168 | + public static byte[] hex2byte(byte[] b) { | ||
169 | + if ((b.length % 2) != 0) | ||
170 | + throw new IllegalArgumentException("长度不是偶数"); | ||
171 | + byte[] b2 = new byte[b.length / 2]; | ||
172 | + for (int n = 0; n < b.length; n += 2) { | ||
173 | + String item = new String(b, n, 2); | ||
174 | + b2[n / 2] = (byte) Integer.parseInt(item, 16); | ||
175 | + } | ||
176 | + return b2; | ||
177 | + } | ||
178 | + | ||
179 | + public static String byte2hex(byte[] b) { | ||
180 | + String hs = ""; | ||
181 | + String stmp = ""; | ||
182 | + for (int n = 0; n < b.length; n++) { | ||
183 | + stmp = (Integer.toHexString(b[n] & 0XFF)); | ||
184 | + if (stmp.length() == 1) | ||
185 | + hs = hs + "0" + stmp; | ||
186 | + else | ||
187 | + hs = hs + stmp; | ||
188 | + } | ||
189 | + return hs.toUpperCase(); | ||
190 | + } | ||
191 | +} |
1 | package com.xkl.inneruse.qrcode; | 1 | package com.xkl.inneruse.qrcode; |
2 | 2 | ||
3 | +import com.utils.DESTools; | ||
3 | import com.xkl.*; | 4 | import com.xkl.*; |
4 | import org.json.JSONObject; | 5 | import org.json.JSONObject; |
5 | import org.junit.Assert; | 6 | import org.junit.Assert; |
@@ -20,6 +21,8 @@ public class QrCodeTest { | @@ -20,6 +21,8 @@ public class QrCodeTest { | ||
20 | public static final String URL_GETQR_WITHOPNEID = Constants.URL_PREFIX + "/qrcode/getQrWithOpenId"; | 21 | public static final String URL_GETQR_WITHOPNEID = Constants.URL_PREFIX + "/qrcode/getQrWithOpenId"; |
21 | public static final String URL_GETUSRINFO_WITHQR = Constants.URL_PREFIX + "/qrcode/getUsrInfoWithQr"; | 22 | public static final String URL_GETUSRINFO_WITHQR = Constants.URL_PREFIX + "/qrcode/getUsrInfoWithQr"; |
22 | 23 | ||
24 | + private static final String HTTP_PARA_QR_KEY = "tnvkqybFHp69pinDZJ7UWuX7"; | ||
25 | + | ||
23 | 26 | ||
24 | /** | 27 | /** |
25 | * 1 .使用用户名密码得到qrcode | 28 | * 1 .使用用户名密码得到qrcode |
@@ -72,13 +75,19 @@ public class QrCodeTest { | @@ -72,13 +75,19 @@ public class QrCodeTest { | ||
72 | */ | 75 | */ |
73 | @Test | 76 | @Test |
74 | public void testGetUsrInfoWithQr() { | 77 | public void testGetUsrInfoWithQr() { |
78 | + try { | ||
75 | String qrcode = getQrCode(); | 79 | String qrcode = getQrCode(); |
76 | Map<String, String> params = new HashMap<String, String>(); | 80 | Map<String, String> params = new HashMap<String, String>(); |
77 | - params.put("qrcode", qrcode); | ||
78 | - params.put("t", HttpTools.getNow()); | 81 | + String now = HttpTools.getNow(); |
82 | + String encryptQr = DESTools.encrypt(qrcode + now, HTTP_PARA_QR_KEY); | ||
83 | + params.put("t", now); | ||
84 | + params.put("encryptQr", encryptQr); | ||
79 | String response = HttpTools.requestByMap(URL_GETUSRINFO_WITHQR, "GET", params); | 85 | String response = HttpTools.requestByMap(URL_GETUSRINFO_WITHQR, "GET", params); |
80 | System.out.println(response); | 86 | System.out.println(response); |
81 | Assert.assertTrue(RtnCodeTools.verifyCode(response, Constants.CODE_SUCC)); | 87 | Assert.assertTrue(RtnCodeTools.verifyCode(response, Constants.CODE_SUCC)); |
88 | + } catch (Exception e) { | ||
89 | + e.printStackTrace(); | ||
90 | + } | ||
82 | } | 91 | } |
83 | 92 | ||
84 | 93 |
-
Please register or login to post a comment