-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEncrypt.java
103 lines (83 loc) · 2.72 KB
/
Encrypt.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
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
public class Encrypt {
native byte[] encrypt(byte[] _buf);
static {
System.loadLibrary("encrypt");
}
// 获取参数
static Map<String, String> getArgMap(String[] args) {
Map<String, String> map = new HashMap<>();
String key = null, val = null;
for (String tmp : args) {
if (tmp.startsWith("-")) {
if (key != null) {
map.put(key, val);
}
key = tmp;
val = null;
} else {
val = tmp;
}
}
if (key != null) {
map.put(key, val);
}
return map;
}
public static void main(String[] args) throws Exception {
Map<String, String> map = getArgMap(args);
String src_name = map.get("-src");
if (src_name == null) {
System.out.println("usage: java Encrypt -src xxx.jar");
return;
}
Encrypt coder = new Encrypt();
String dst_name = map.get("-dst");
if (dst_name == null || dst_name.equals(src_name)) {
dst_name = src_name.substring(0, src_name.length() - 4) + "_encrypt.jar";
}
System.out.printf("encode jar file: [%s ==> %s ]\n", src_name, dst_name);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
File dst_file = new File(dst_name);
File src_file = new File(src_name);
FileOutputStream dst_fos = new FileOutputStream(dst_file);
JarOutputStream dst_jar = new JarOutputStream(dst_fos);
JarFile src_jar = new JarFile(src_file);
for (Enumeration<JarEntry> enumeration = src_jar.entries(); enumeration.hasMoreElements(); ) {
JarEntry entry = enumeration.nextElement();
InputStream is = src_jar.getInputStream(entry);
int len;
while ((len = is.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, len);
}
byte[] bytes = baos.toByteArray();
String name = entry.getName();
if (name.endsWith(".class") && name.startsWith("org/XXX/XX")) {
System.out.println("encrypt " + name.replaceAll("/", "."));
try {
bytes = coder.encrypt(bytes);
} catch (Exception e) {
System.out.println("encrypt error happend~");
e.printStackTrace();
}
}
JarEntry ne = new JarEntry(name);
dst_jar.putNextEntry(ne);
dst_jar.write(bytes);
baos.reset();
}
src_jar.close();
dst_jar.close();
dst_fos.close();
}
}