• 自定义ClassLoader加载加密的class文件


    package com.yd.wmsc.util;
    
    public class Test {
           public void say(){
                System.out.println("Say Hello");
            }
    }
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    
    public class FileUtils {
    
        public static void test(String path){
            File file = new File(path);
            try {
                FileInputStream fis = new FileInputStream(file);
                FileOutputStream fos = new FileOutputStream(path+"en");
                int b = 0;
                int b1 = 0;
                try {
                    while((b = fis.read()) != -1){
                        //每一个byte异或一个数字2
                        fos.write(b ^ 2);
                    }
                    fos.close();
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    
    
    public class DeClassLoader extends ClassLoader {
    
        private String mLibPath;
    
        public DeClassLoader(String path) {
            // TODO Auto-generated constructor stub
            mLibPath = path;
        }
    
        @Override
        protected Class<?> findClass(String name) throws ClassNotFoundException {
            // TODO Auto-generated method stub
    
            String fileName = getFileName(name);
    
            File file = new File(mLibPath,fileName);
    
            try {
                FileInputStream is = new FileInputStream(file);
    
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                int len = 0;
                byte b = 0;
                try {
                    while ((len = is.read()) != -1) {
                        //将数据异或一个数字2进行解密
                        b = (byte) (len ^ 2);
                        bos.write(b);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                byte[] data = bos.toByteArray();
                is.close();
                bos.close();
    
                return defineClass(name,data,0,data.length);
    
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return super.findClass(name);
        }
    
        //获取要加载 的class文件名
        private String getFileName(String name) {
            // TODO Auto-generated method stub
            int index = name.lastIndexOf('.');
            if(index == -1){ 
                return name+".classen";
            }else{
                return name.substring(index+1)+".classen";
            }
        }
    
    }
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    public class ClassLoaderTest {
    
        public static void main(String[] args) {
            FileUtils.test("D:\Test.class");
            DeClassLoader diskLoader = new DeClassLoader("D:\");
            try {
                //加载class文件
                Class c = diskLoader.loadClass("com.yd.wmsc.util.Test");
    
                if(c != null){
                    try {
                        Object obj = c.newInstance();
                        Method method = c.getDeclaredMethod("say",null);
                        //通过反射调用Test类的say方法
                        method.invoke(obj, null);
                    } catch (InstantiationException | IllegalAccessException 
                            | NoSuchMethodException
                            | SecurityException | 
                            IllegalArgumentException | 
                            InvocationTargetException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
  • 相关阅读:
    通过反射获取Android通知栏高度
    Android 的EditText实现不可编辑
    Android 代码混淆、第三方平台加固加密、渠道分发 完整教程(图文)
    eclipse、myeclipse,svn插件subclipse 忘记密码的解决方法(win7、win8、xp)
    Eclipse 基于接口编程的时候,快速跳转到实现类的方法(图文)
    WIN7、WIN8 右键在目录当前打开命令行Cmd窗口(图文)
    eclipse,myeclipse开发环境下,maven远程部署到tomcat7服务器(图文)
    python之路——博客目录
    生成器和协程 —— 你想知道的都在这里了
    python3.7导入gevent模块报错的解决方案
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/7324710.html
Copyright © 2020-2023  润新知