自定义加密的类加载器
1、加载器初识
JVM有有三种类加载器:
*BootStrap加载JRE\lib\rt.jar
*ExtClassLoader加载JRE/lib/ext/*.jar
*AppClassLoader加载
classpath指定目录下的类
2、自定义加载器
1)被加载的类
public class ClassAttachment {
public ClassAttachment() {
System.out.println("Hello,World");
}
}
2)加密class的加密器
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Cypher {
public static void main(String[] args) throws IOException {
String srcPath =
"D:\\Android\\reflect\\bin\\classloader\\ClassAttachment.class";
String destPath = "cypherClass";
String fileName = srcPath.substring(srcPath.lastIndexOf('\\') + 1);
String destFilePath = destPath + "\\" + fileName;
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(destFilePath);
cypher(fis, fos);
fis.close();
fos.close();
}
// 进行简单加密
public static void cypher(InputStream is, OutputStream os) {
int b;
try {
while ((b = is.read()) != -1) {
os.write(b ^ 0xff);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
3)自定义的加载器
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class MyClassLoader extends ClassLoader {
// 指定存放加密的class的目录
private String classDir;
public MyClassLoader() {
}
public MyClassLoader(String classDir) {
this.classDir = classDir;
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
// 生成加密class文件信息
String classFileName =
classDir + "\\" + name.substring(name.lastIndexOf('.') + 1) + ".class";
try {
System.out.println("-----through MyClassLoader-----");
FileInputStream fis = new FileInputStream(classFileName);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 解密
Cypher.cypher(fis, baos);
byte[] classByte = baos.toByteArray();
fis.close();
baos.close();
// 根据byte[]生成Class
return defineClass(null, classByte, 0, classByte.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return super.findClass(name);
}
}
4)测试自定义的加载器
public class MyClassLoaderTest {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class clazz =
new MyClassLoader("cypherClass").loadClass("classloader.ClassAttachment");
clazz.newInstance();
}
}
测试方法:
新建一个cypherClass目录,通过加密器加密自动编译的ClassAttachment.class,用它覆盖掉原来的.class文件,由于,类加载的过程中,会先让上一级的加载器进行加载,如果,上一级的加载器没有找到要加载的class文件,才会让下一级的类加载器进行加载,由于AppClassLoader现在可以找到加密后的ClassAttachment.class文件,所以,会出现如下
错误:
[color=red]
java.lang.ClassFormatError: Incompatible magic value 889275713 in class file classloader/ClassAttachment
[/color]
如果将原目录下的ClassAttachment.class文件删除掉,即让AppClassLoader找不到ClassAttachment.class文件,让下一级的类加载器进行加载,即自定义的加载器加载,程序运行成功:
运行结果:
-----through MyClassLoader-----
Hello,World