当前位置:网站首页>自定义类加载器对类加密解密
自定义类加载器对类加密解密
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
边栏推荐
- How to measure the thickness of glass substrate by spectral confocal
- Reading graph augmentations to learn graph representations (lg2ar)
- Great health industry annual must attend event, 2022 Shandong International Great Health Industry Expo
- JS library for number formatting
- find_circ详细使用指南
- Mindspire, a domestic framework, cooperates with Shanshui nature conservation center to find and protect the treasure life in the "China water tower"
- redis详细教程
- 小白看MySQL--windows环境安装MySQL
- Operating instructions and Q & A of cec-i China learning machine
- Record a bug caused by a line break
猜你喜欢

Beyond lithium battery -- the concept of battery in the future

如何把老式键盘转换成USB键盘并且自己编程?

温故知新--常温常新

Statistical Hypothesis Testing

Com. Faster XML. Jackson. DataBind. Exc.mismatchedinputexception: tableau ou chaîne attendu. At [Source: X

buuctf-pwn write-ups (6)

Mindspire, a domestic framework, cooperates with Shanshui nature conservation center to find and protect the treasure life in the "China water tower"

高清滑环生产过程当中的质量如何把控

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

【Mysql】时间字段默认设置为当前时间
随机推荐
Esp32 add multi directory custom component
统一结果集的封装
The most difficult 618 in history, TCL won the first place in both jd.com and tmall.com shares in the TV industry
How to convert an old keyboard into a USB keyboard and program it yourself?
网上开通证券账户安全吗 手机炒股靠谱吗
Memorizing byte order of big and small end
The world is very big. Some people tattoo QR codes on their necks
In depth understanding of UDP in the transport layer and the use of UDP in sockets
memcached基础5
Oracle database basics concepts
Moher College -x-forwarded-for injection vulnerability practice
Solve the problem that only one line of text is displayed or not displayed in u8glib
07 | workflow design: how to design a reasonable multi person development mode?
JSON解析,ESP32轻松获取时间气温和天气
超越锂电池——未来电池的概念
redis详细教程
一键加速索尼相机SD卡文件的复制操作,文件操作批处理教程
05 | 规范设计(下):commit 信息风格迥异、难以阅读,如何规范?
Special topic II on mathematical physics of the sprint strong foundation program
How to measure the thickness of glass substrate by spectral confocal