当前位置:网站首页>自定义类加载器对类加密解密
自定义类加载器对类加密解密
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
边栏推荐
- 解决STC8G1K08程序不能运行的问题和端口配置
- “message“:“Bad capabilities. Specify either app or appTopLevelWindow to create a session“
- 论文解读(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》
- Is it safe to open a securities account online? Is it reliable to speculate in stocks by mobile phone
- 2022健康博览会,山东养生保健展会,产后健康、睡眠健康展
- 墨者学院-SQL注入漏洞测试(报错盲注)
- 07 | 工作流设计:如何设计合理的多人开发模式?
- Play OLED, u8g2 animation, increasing numbers, random triangles, etc
- Batch generate folders based on file names
- Database interview questions +sql statement analysis
猜你喜欢

com. fasterxml. jackson. databind. exc.MismatchedInputException: Expected array or string. at [Source:x

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

Batch generate folders based on file names

LeetCode 142. Circular linked list II

TopoLVM: 基于LVM的Kubernetes本地持久化方案,容量感知,动态创建PV,轻松使用本地磁盘

Timing mechanism of LwIP

简单快速的数网络(网络中的网络套娃)

3线spi屏幕驱动方式

直播回顾 | 子芽&CCF TF:云原生场景下软件供应链风险治理技术浅谈

Oracle 數據庫基本知識概念
随机推荐
光谱共焦如何测量玻璃基板厚度
Encapsulation of unified result set
What is the difference between the working principle of gas-liquid slip ring and other slip rings
JSON解析,ESP32轻松获取时间气温和天气
滑环安装有哪些技巧和方法
Network in network (dolls)
Moher College - SQL injection vulnerability test (error reporting and blind note)
[UVM actual battle== > episode_3] ~ assertion, sequence, property
Play OLED, u8g2 animation, increasing numbers, random triangles, etc
ESP32实验-自建web服务器配网02
Record a bug caused by a line break
Is it safe to open a compass account?
Concepts de base de données Oracle
XML learning notes
How to control the quality of HD slip ring in the production process
JSON parsing, esp32 easy access to time, temperature and weather
其他服务注册与发现
The world is very big. Some people tattoo QR codes on their necks
ESP32-添加多目录的自定义组件
Custom MVC (imported into jar package) + difference from three-tier architecture + reflection + interview questions