-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMemeory.java
90 lines (69 loc) · 2.98 KB
/
Memeory.java
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
import java.net.CookieManager;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Memeory {
private static HttpClient client;
private static MessageDigest messageDigest;
private final static String BASE = "http://whale.hacking-lab.com:1111/";
private final static String PIC = "pic/";
private final static String SOLVE = "solve";
private static String convertByteToHex(byte[] byteData) {
StringBuilder sb = new StringBuilder();
for (byte byteDatum : byteData) {
sb.append(Integer.toString((byteDatum & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
private static void playRound() throws Exception {
Map<String, Integer> hashTable = new HashMap<>();
List<Pair> pairs = new ArrayList<>();
HttpRequest roundRequest = HttpRequest.newBuilder(URI.create(BASE)).GET().build();
client.send(roundRequest, HttpResponse.BodyHandlers.ofString());
for (int i = 1; i <= 98; i++) {
HttpRequest imageRequest = HttpRequest.newBuilder(URI.create(BASE + PIC + i)).GET().build();
byte[] body = client.send(imageRequest, HttpResponse.BodyHandlers.ofByteArray()).body();
String imageHash = convertByteToHex(messageDigest.digest(body));
if (hashTable.containsKey(imageHash)) {
Integer previous = hashTable.get(imageHash);
pairs.add(new Pair(previous, i));
} else {
hashTable.put(imageHash, i);
}
}
for (Pair pair : pairs) {
HttpRequest solveRequest = HttpRequest.newBuilder(URI.create(BASE + SOLVE))
.header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
.POST(HttpRequest.BodyPublishers.ofString("first=" + pair.first + "&second=" + pair.second))
.build();
HttpResponse<String> solveResponse = client.send(solveRequest, HttpResponse.BodyHandlers.ofString());
System.out.println(solveResponse.body());
}
}
public static void main(String[] args) throws Exception {
CookieManager.setDefault(new CookieManager());
client = HttpClient.newBuilder()
.cookieHandler(CookieManager.getDefault())
.followRedirects(HttpClient.Redirect.ALWAYS)
.build();
messageDigest = MessageDigest.getInstance("MD5");
for (int i = 0; i < 10; i++) {
System.out.println("Playing round " + i);
playRound();
}
}
private static final class Pair {
private Integer first;
private Integer second;
private Pair(Integer first, Integer second) {
this.first = first;
this.second = second;
}
}
}