当前位置:网站首页>Custom class loader encrypts and decrypts classes
Custom class loader encrypts and decrypts classes
2022-06-27 01:00:00 【wfsm】
If you need to write your own class loader , Just inherit
ClassLoaderclass , rewritefindClass(String className)Method
ClassLoader Superclass
loadClass()Method is used to delegate the loading operation of a class to the parent class loader for execution , If the parent class loader cannot load this class , This class loader will be calledfindClass()loadClass()This is where the logic of the parental delegation model is implemented , Modifying this method without authorization will cause the model to be destroyed , So in order to ensure that the parental delegation model is not broken , It is recommended not to rewriteloadClass(), But infindClass()Methods that override custom classes in
public class User {
private String name = "Rocky";
private int age = 18;
}
Encryption and decryption tool class :
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() {
}
/** * Decrypt : Use your own 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);
}
}
Problems encountered :
NoClassDefFoundError: User (wrong name: security/User)
Compiled User With package name , The package name is also required for parsingjava.lang.IllegalArgumentException: character to be escaped is missing

quote :https://blog.csdn.net/rockvine/article/details/124836389
https://blog.csdn.net/weixin_34321977/article/details/91658732
边栏推荐
- 简单快速的数网络(网络中的网络套娃)
- 2022 Health Expo, Shandong health care exhibition, postpartum health and sleep health exhibition
- IIS 部署静态网站和 FTP 服务
- Record a bug caused by a line break
- Xiaobai looks at MySQL -- installing MySQL in Windows Environment
- 一键加速索尼相机SD卡文件的复制操作,文件操作批处理教程
- Interface test framework practice (I) | requests and interface request construction
- Central Limit Theorem
- Concepts de base de données Oracle
- 史上最难618,TCL夺得电视行业京东和天猫份额双第一
猜你喜欢

滑环选型选购时需要注意的技巧

Unable to create a folder to save the sketch: MKDIR sketch

Lwip之定时机制

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

Other service registration and discovery

Redis detailed tutorial

How to measure the thickness of glass substrate by spectral confocal

07 | 工作流设计:如何设计合理的多人开发模式?
![The [MySQL] time field is set to the current time by default](/img/40/5f1d3448259ab703c4b5dc29713a99.png)
The [MySQL] time field is set to the current time by default

Is there anyone who doesn't know the three cores of concurrent programming?
随机推荐
Central Limit Theorem
Keepalived 实现 Redis AutoFailover (RedisHA)16
Interface test framework practice (I) | requests and interface request construction
30《MySQL 教程》MySQL 存储引擎概述
memcached基础5
memcached基础
超越锂电池——未来电池的概念
对象的访问机制及其他
根据文件名批量生成文件夹
About Random Numbers
C#程序结构预览最基础入门
Gaussian and Summary Stats
建模规范:环境设置
Keepalived 实现 Redis AutoFailover (RedisHA)11
如何写好测试用例以及go单元测试工具testify简单介绍
Oracle 數據庫基本知識概念
Is there anyone who doesn't know the three cores of concurrent programming?
光谱共焦如何测量玻璃基板厚度
memcached基础4
一键加速索尼相机SD卡文件的复制操作,文件操作批处理教程