第十篇 JVM核心机制之JVM运行和类加载全过程(五)

时间:2022-12-21 10:39:09

加密、解密操作

加密操作

 

 1 /**
 2  * 
 3  * 加密操作
 4  *
 5  * @author Zhang XiaoDao
 6  *
 7  */
 8 public class EncrpUtil {
 9     
10     public static void main(String[] args) {
11         encrp("d:/java/Hello.class", "d:/java/temp/Hello.class");
12     }
13     
14     public static void encrp(String src,String dest){
15         FileInputStream fileInputStream = null;
16         FileOutputStream fileOutputStream = null;
17         try {
18             fileInputStream = new FileInputStream(src);
19             fileOutputStream = new FileOutputStream(dest);
20             
21             int temp = -1;
22             if((temp=fileInputStream.read()) != -1){
23                 fileOutputStream.write(temp^0xff);//取反操作
24             }
25         } catch (Exception e) {
26             
27         }finally {
28             if(fileInputStream != null){
29                 try {
30                     fileInputStream.close();
31                 } catch (IOException e) {                    
32                     e.printStackTrace();
33                 }
34             }
35             
36             if(fileOutputStream != null){
37                 try {
38                     fileOutputStream.close();
39                 } catch (IOException e) {                    
40                     e.printStackTrace();
41                 }
42             }
43         }
44         
45     }
46 }

 

 

解密操作

 

 1 /**
 2  * 
 3  * 解密操作
 4  *
 5  * @author Zhang XiaoDao
 6  *
 7  */
 8 public class DecrptClassLoader extends ClassLoader{
 9     
10     private String rootDir;
11 
12     public DecrptClassLoader(String rootDir) {
13         super();
14         this.rootDir = rootDir;
15     }
16     
17     @Override
18     protected Class<?> findClass(String name) throws ClassNotFoundException {
19         Class<?> c = findLoadedClass(name);
20         //应该要先查询有没有加载过这个类,如果已经加载,则直接返回这个类。如果没有加载,则返回新的类
21         if(c != null){
22             return c;
23         }else{
24             ClassLoader parent = this.getParent();
25             c = parent.loadClass(name);
26             if(c != null){
27                 return c;
28             }else{
29                 byte[] classData = getClassData(name);
30                 if(classData == null){
31                     throw new ClassNotFoundException();
32                 }else{
33                     c = defineClass(name, classData, 0, classData.length);
34                 }
35             }
36         }
37         return c;
38     }
39     
40     private byte[] getClassData(String classname){//com.bjsxt.test.User  --> d:/java/ com/bjsxt/test/User.class
41         String path = rootDir + "/" + classname.replace('.', '/') + ".class";
42         //IOUtils 可以使用它将流中的数据转成字节数组
43         InputStream is = null;
44         ByteArrayOutputStream  baOutputStream = new ByteArrayOutputStream();
45         try {
46             is = new FileInputStream(path);
47             int temp = -1;
48             if((temp=is.read()) != -1){
49                 baOutputStream.write(temp^0xff);//取反操作
50             }
51             return baOutputStream.toByteArray();
52         } catch (Exception e) {
53             e.printStackTrace();
54             return null;
55         }finally {
56             if(is != null){
57                 try {
58                     is.close();
59                 } catch (IOException e) {
60                     e.printStackTrace();
61                 }
62             }
63             if(baOutputStream != null){
64                 try {
65                     baOutputStream.close();
66                 } catch (IOException e) {
67                     e.printStackTrace();
68                 }
69             }
70         }        
71 //        return null;        
72     }
73     
74 }

 

 

测试加密解密

 

 1 /**
 2  * 
 3  * 测试简单的加密解密(取反)操作
 4  *
 5  * @author Zhang XiaoDao
 6  *
 7  */
 8 public class Demo04 {
 9     public static void main(String[] args) throws Exception {
10         /*int a = 3;//00000011
11         System.out.println(Integer.toBinaryString(a^0xff));*/
12         //测试取反操作,加密后的class文件,正常的类加载器无法加载,报classFormatError
13         /*FileSystemClassLoader loader = new FileSystemClassLoader("d:/java/temp");        
14         Class<?> c1 = loader.loadClass("Hello");
15         System.out.println(c1);*/
16         
17         //加密操作,可以加载到类
18         DecrptClassLoader loader = new DecrptClassLoader("d:/java/temp");
19         Class<?> c1 = loader.loadClass("Hello");
20         System.out.println(c1);
21     }
22 }