JkToken.java
3.19 KB
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
package com.bsth.serviec_jk;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;
public class JkToken {
//加密的token码
public static String jkdata() {
try {
String token ="abfed98867c64fde99f7f88a4532efa8";
String endpoint = "https://180.167.126.126:8992/api/getcode";
InputStream in = null;
try {
HttpURLConnection con = (HttpURLConnection) new URL(endpoint).openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("GET");
con.setConnectTimeout(10000);
con.setReadTimeout(10000);
con.setRequestProperty("keep-alive", "true");
con.setRequestProperty("accept", "application/json");
con.setRequestProperty("content-type", "application/json");
if (con.getResponseCode() == 200) {
in = con.getInputStream();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
IOUtils.copy(in, bout);
Map map = new ObjectMapper().readValue(bout.toByteArray(), Map.class);
String sd = encrypt(token,(String)map.get("result"));
return sd;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String encrypt(String token, String code) throws Exception {
if (token.length() != 32) throw new Exception("token 长度不正确");
code = padRight(code, 16, '0');
String tmpToken = "";
for (int i = 0; i < 8; i++){
String subString = code.substring(i * 2, (i + 1) * 2);
tmpToken += subString + token.substring(i * 4, (i + 1) * 4);
}
int pos = tmpToken.length() / 2;
tmpToken = tmpToken.substring(pos) + tmpToken.substring(0, pos);
pos = tmpToken.length() / 4;
tmpToken = tmpToken.substring(pos * 3) + tmpToken.substring(pos, pos * 3) + tmpToken.substring(0, pos);
return URLEncoder.encode(tmpToken, "GBK");
}
public static String padRight(String s, int len, char ch){
int diff = len - s.length();
if (diff <= 0) {
return s;
}
char[] charr = new char[len];
System.arraycopy(s.toCharArray(), 0, charr, 0, s.length());
for (int i = s.length(); i < len; i++) {
charr[i] = ch;
}
return new String(charr);
}
public static void main(String[] args) {
jkdata();
}
}