当前位置:网站首页>自定义类加载器对类加密解密
自定义类加载器对类加密解密
2022-06-27 00:28:00 【wfsm】
如果需要编写自己的类加载器,只需要继承
ClassLoader类,重写findClass(String className)方法
ClassLoader超类的
loadClass()方法用于将类的加载操作委托给父类加载器去执行,如果这个父类加载器无法加载这个类,就会调用这个类加载器的findClass()loadClass()是实现双亲委派模型逻辑的地方,擅自修改这个方法会导致模型被破坏,所以为了保证双亲委派模型不被破坏,建议不要重写loadClass(),而是在findClass()中重写自定义类的方法
public class User {
private String name = "Rocky";
private int age = 18;
}
加密解密工具类:
public class XorEncryptUtil {
private static void xor(InputStream in, OutputStream out) throws IOException {
int ch;
while ((ch = in.read()) != -1){
ch = ch^ 0xff;
out.write(ch);
}
}
public static void encrypt(File src, File des) throws IOException {
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(des);
xor(in,out);
in.close();
out.close();
}
public static byte[] decrypt(File src) throws IOException {
FileInputStream in = new FileInputStream(src);
ByteArrayOutputStream out = new ByteArrayOutputStream();
xor(in,out);
byte[] data = out.toByteArray();
in.close();
out.close();
return data;
}
public static void main(String[] args) throws IOException {
File src = new File("C:\\Users\\Administrator\\Desktop\\sb\\1.txt");
File des = new File("C:\\Users\\Administrator\\Desktop\\sb\\2.txt");
XorEncryptUtil.encrypt(src,des);
byte[] decrypt = XorEncryptUtil.decrypt(des);
String s = new String(decrypt);
System.out.println("s = " + s);
}
}
classLoader:
public class MyClassLoader extends ClassLoader {
private String basePath;
private final static String FILE_EXT = ".class";
public MyClassLoader(String basePath) {
this.basePath = basePath;
}
public MyClassLoader() {
}
/** * 解密: 使用自己的ClassLoader * @param className * @return */
private byte[] loadClassData(String className){
try {
// String tempName = className.replaceAll("\\.", System.getProperty("file.separator"));
String tempName = className.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
File targetFile = new File(basePath + tempName + FILE_EXT);
return XorEncryptUtil.decrypt(targetFile);
} catch (IOException e) {
System.out.println("silly b");
e.printStackTrace();
}
return null;
}
@Override
protected Class<?> findClass(String className) throws ClassNotFoundException {
byte[] data = this.loadClassData(className);
return this.defineClass(className, data, 0, data.length);
}
public static void main(String[] args) throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException {
XorEncryptUtil.encrypt(new File("I:\\java\\springsecurity\\springsecuritylearn\\security\\target\\classes\\security\\User.class"),
new File("C:\\Users\\Administrator\\Desktop\\sb\\User.class"));
MyClassLoader myClassLoader = new MyClassLoader();
myClassLoader.basePath = "C:\\Users\\Administrator\\Desktop\\sb\\";
Class<?> clazz = myClassLoader.findClass("security.User");
System.out.println(clazz.getClassLoader());
Object o = clazz.newInstance();
System.out.println("o = " + o);
}
}
遇到的问题:
NoClassDefFoundError: User (wrong name: security/User)
被编译的User有包名,解析的时候也需要包名java.lang.IllegalArgumentException: character to be escaped is missing

引用:https://blog.csdn.net/rockvine/article/details/124836389
https://blog.csdn.net/weixin_34321977/article/details/91658732
边栏推荐
- CPU exception handling
- 论文解读(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》
- How to measure the thickness of glass substrate by spectral confocal
- find_ Detailed use guide of CIRC
- 超越锂电池——未来电池的概念
- Interface test framework practice (I) | requests and interface request construction
- One click acceleration of Sony camera SD card file copy operation, file operation batch processing tutorial
- Operating instructions and Q & A of cec-i China learning machine
- XSS攻击笔记(上)
- Find the minimum value in the rotation sort array ii[classical Abstract dichotomy + how to break the game left, middle and right are equal]
猜你喜欢

Solve the problem that stc8g1k08 program cannot run and port configuration

解决STC8G1K08程序不能运行的问题和端口配置
![Find the minimum value in the rotation sort array ii[classical Abstract dichotomy + how to break the game left, middle and right are equal]](/img/75/05d5765588dfde971167fbc72e2aa8.png)
Find the minimum value in the rotation sort array ii[classical Abstract dichotomy + how to break the game left, middle and right are equal]

Bootstrapblazor + FreeSQL actual combat chart usage (2)

3-wire SPI screen driving mode

自定义MVC(导成jar包)+与三层架构的区别+反射+面试题

Live review | Ziya &ccf TF: Discussion on software supply chain risk management technology under cloud native scenario

Gaussian and Summary Stats

USB协议中HID设备描述符以及键盘按键值对应编码表

ArcGIS 镶嵌数据集切片丢失问题处理
随机推荐
用代码生成流程图,Markdown的使用方法
[vscade] preview MD file
BootstrapBlazor + FreeSql实战 Chart 图表使用(2)
统计无向图中无法互相到达点对数[经典建邻接表+DFS统计 -> 并查集优化][并查集手册/写的详细]
At present, which securities company is the best and safest to open an account for stock speculation?
Live review | Ziya &ccf TF: Discussion on software supply chain risk management technology under cloud native scenario
Hid device descriptor and keyboard key value corresponding coding table in USB protocol
How to measure the thickness of glass substrate by spectral confocal
墨者学院-SQL注入漏洞测试(报错盲注)
How to write test cases and a brief introduction to go unit test tool testify
matlab数据类型 —— 字符型
The most difficult 618 in history, TCL won the first place in both jd.com and tmall.com shares in the TV industry
redis详细教程
Redis detailed tutorial
3-wire SPI screen driving mode
如何把老式键盘转换成USB键盘并且自己编程?
memcached基础5
com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string. at [Source:x
Is it safe for CITIC Securities Commission to open an online account and speculate in stocks
Batch generate folders based on file names