当前位置:网站首页>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
ClassLoader
class , 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
边栏推荐
猜你喜欢
07 | workflow design: how to design a reasonable multi person development mode?
【Mysql】时间字段默认设置为当前时间
XSS攻击笔记(上)
2022年地理信息系统与遥感专业就业前景与升学高校排名选择
One click acceleration of Sony camera SD card file copy operation, file operation batch processing tutorial
基于SSMP的宠物医院管理系统
Count the logarithm of points that cannot reach each other in an undirected graph [classic adjacency table building +dfs Statistics - > query set optimization] [query set manual / write details]
How to convert an old keyboard into a USB keyboard and program it yourself?
MATLAB data type - character type
Flink 实战问题(七):No Watermark(Watermarks are only available EventTime is used)
随机推荐
XML learning notes
Custom jsp[if, foreach, data, select] tag
寻找旋转排序数组中的最小值 II[经典抽象二分 + 如何破局左中右三者相等]
MATLAB data type - character type
CH423要如何使用,便宜的国产IO扩展芯片
Esp32 add multi directory custom component
Live review | Ziya &ccf TF: Discussion on software supply chain risk management technology under cloud native scenario
超越锂电池——未来电池的概念
3 - wire SPI Screen Drive
JSON解析,ESP32轻松获取时间气温和天气
TopoLVM: 基于LVM的Kubernetes本地持久化方案,容量感知,动态创建PV,轻松使用本地磁盘
From bitmap to bloom filter, C # implementation
memcached基础6
Keepalived 实现 Redis AutoFailover (RedisHA)14
小白看MySQL--windows环境安装MySQL
建模规范:环境设置
Count the logarithm of points that cannot reach each other in an undirected graph [classic adjacency table building +dfs Statistics - > query set optimization] [query set manual / write details]
Other service registration and discovery
Is it safe to open a compass account?
memcached基础