类加解密 & 自定义类加载器
# 类加解密工具
package com.xiangxue.classloader;
import java.io.*;
/**
* @version 1.0
* @program: springTest
* @description: 类的加解密工具
* @author: Kevin
* @create: 2019-09-10 14:00
**/
public class EDClassUtils {
///异或操作, 可以进行加密、解密
public void xor(InputStream inputStream, OutputStream outputStream) throws IOException {
int ch;
while (-1 != (ch = inputStream.read())){
ch = ch^ 0xff;
outputStream.write(ch);
}
}
public void encrypt(File src, File dest) throws IOException {
InputStream inputStream = new FileInputStream(src);
OutputStream outputStream = new FileOutputStream(dest);
try {
xor(inputStream, outputStream);
} catch (IOException e) {
e.printStackTrace();
}finally {
inputStream.close();
outputStream.close();
}
}
//解密方法
public byte[] decode(File src) throws IOException {
InputStream inputStream = new FileInputStream(src);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
xor(inputStream, byteArrayOutputStream);
byte[] data = byteArrayOutputStream.toByteArray();
return data;
}
public static void main(String[] args) {
EDClassUtils edClassUtils = new EDClassUtils();
File src = new File("D:\\gitcloneTest\\springTest\\target\\classes\\com\\xiangxue\\io\\aio\\ServerEx.class");
File dest = new File("D:\\gitcloneTest\\springTest\\target\\classes\\com\\xiangxue\\io\\aio\\Server.class");
System.out.println("加密完成");
try {
edClassUtils.encrypt(src, dest);
} catch (IOException e) {
e.printStackTrace();
}
try {
edClassUtils.decode(dest);
} catch (IOException e) {
e.printStackTrace();
}
}
}
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
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
# 自定义类加载器
package com.xiangxue.classloader;
import java.io.File;
import java.io.IOException;
/**
* @version 1.0
* @program: springTest
* @description: 自定义的类加载器
* @author: Kevin
* @create: 2019-09-10 14:02
**/
public class CustomClassLoader extends ClassLoader {
private String bashPath;
private static final String FILE_EXT = ".class";
public CustomClassLoader(String bashPath) {
this.bashPath = bashPath;
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
byte[] data = this.loadClassData(name);
return this.defineClass(name, data, 0, data.length);
}
private byte[] loadClassData(String name) {
byte[] data = null;
EDClassUtils edClassUtils = new EDClassUtils();
String tempName = name.replaceAll("\\.", "\\\\");
try {
data = edClassUtils.decode(new File(bashPath + tempName + FILE_EXT));
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
}
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
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
# 测试类
package com.xiangxue.classloader;
/**
* @version 1.0
* @program: springTest
* @description:
* @author: Kevin
* @create: 2019-09-10 14:20
**/
public class Test {
public static void main(String[] args) {
CustomClassLoader customClassLoader =new CustomClassLoader("D:\\gitcloneTest\\springTest\\target\\classes\\");
try {
Class<?> serverEx = customClassLoader.findClass("com.xiangxue.io.aio.Server");
System.out.println(serverEx.getClassLoader());
Object o = serverEx.newInstance();
System.out.println(o);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
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
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
# 测试结果
com.xiangxue.classloader.CustomClassLoader@63947c6b
com.xiangxue.io.aio.Server@355da254
1
2
2
编辑 (opens new window)
上次更新: 2022/03/08, 00:59:33