Skip to content

Commit 3381dd2

Browse files
authored
load and decrypt class file (#30)
merge
1 parent 986489d commit 3381dd2

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,52 @@
11
package club.shengsheng;
22

33

4+
import java.io.ByteArrayOutputStream;
5+
import java.io.FileInputStream;
6+
import java.io.IOException;
7+
48
/**
59
* @author gongxuanzhangmelt@gmail.com
610
**/
711
public class MyClassLoader extends ClassLoader {
812

13+
private static final String classFile = "加密.class";
14+
15+
@Override
16+
public Class<?> loadClass(String name) throws ClassNotFoundException {
17+
if (name != null && name.startsWith("java.")) {
18+
return super.loadClass(name);
19+
}
20+
return findClass(name);
21+
}
22+
23+
@Override
24+
protected Class<?> findClass(String name) throws ClassNotFoundException {
25+
byte[] classData = null;
26+
try {
27+
classData = readClassFile();
28+
} catch (IOException e) {
29+
throw new ClassNotFoundException("Could not read class file: " + classFile);
30+
}
31+
return defineClass(name, classData, 0, classData.length);
32+
}
33+
34+
private byte[] readClassFile() throws IOException {
35+
try (FileInputStream fis = new FileInputStream(classFile);
36+
ByteArrayOutputStream byteArrStream = new ByteArrayOutputStream()) {
37+
38+
byte[] buffer = new byte[1024];
39+
int bytesRead;
40+
41+
while ((bytesRead = fis.read(buffer)) != -1) {
42+
byteArrStream.write(buffer, 0, bytesRead);
43+
}
44+
45+
byte[] byteArray = byteArrStream.toByteArray();
46+
for (int i = 0; i < byteArray.length; i++) {
47+
byteArray[i] = (byte)(byteArray[i] - 1);
48+
}
49+
return byteArray;
50+
}
51+
}
952
}

0 commit comments

Comments
 (0)