Android进阶:自定义类加载器加载加密类文件

以前面试的时候有许多面试官问类加载器相关的问题,因此这是一个很重要的知识点。并且对于高级Android研发来说,懂得更多类加载相关的东西,对开发也会有不少的帮助,好比热更新,类加密等。 其实笔者对类加密比较感兴趣,就稍稍调研了一下。类加密的实际上是为了防止APP被反编译,防止反编译的方法有不少种,好比混淆,加固等。本身对类文件进行加密,并自定义类加载器也是一种办法:面试

首先咱们的代码打包编译以后会变成难以读懂的二进制字节码,而且变成.class文件。可是简单的APP编译出来以后能够被反编译,甚至你写的代码完彻底全被暴露。你的代码被抄袭,被复制都是小事,重要的实际上是大家APP的商业信息有可能被泄露! 下面是一个简单的例子被反编译的场景: 算法

因此为了避免让用户轻易的反编译出源代码文件,就要对.class文件进行加密,再经过特殊的加载类的方式解密,并将这个类加载到内存中。

首先说加密,加密无非就是把.class字节码文件进行一些变换,这里面就涉及密码学的知识了!加密的方式有不少种,要想提升保密性,能够考虑DES,AES,RSA。一旦加密算法的源码被公开,其实破解也就是很简单的事情了,因此建议你们仍是用高安全性的密码系统,到时候及时的更换密钥。就能必定程度上增长破解难度。安全

有了加密算法,接下来就是加密一个字节码文件了:bash

private static File file = null;
    private static String path = null;
    // 读取已经编译好的正常的class字节码文件
    public static void readClass(String filePath) throws Exception {
        file = new File(filePath);
        path = filePath;
    }

    // 加密生成已加密的class字节码文件
    public static void encrypt() throws Exception {
        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(path.substring(0,
                path.lastIndexOf(".class"))
                + "附件.class");
        byte[] b = new byte[1024];
        int ch = 0;
        while ((ch = fis.read(b)) != -1) {
            // 变换b
            b=crypt(b, "encrypt");
            fos.write(b, 0, ch);
        }
    }
复制代码

这时候,调用readClass方法和encrypt方法,就能在本来XX.class文件的目录下生成XX附件.class。这时候,咱们就使用这个副本的字节码文件,删除原来的,下次运行的时候再解密,这样就好了。ide

对类加密以后,须要用本身的方式把类再加载出来。正常的时候我们写的类也须要被类加载器加载到内存中。因此,就涉及到类加载器的知识了:学习

**系统默认三个类加载器,分别是:**BootStrap,ExtClassLoader,AppClassLoader。那么这几个类加载器有什么区别呢?ui

首先类加载器有父子关系。BootStrap是爷爷(用C++编写,主要负责加载jre/lib/rt.jar),ExtClassLoader是爸爸(主要用于加载JRE/lib/ext/*.jar),AppClassLoader是儿子(用于负责加载ClassPath指定目录下的全部jar)。this

因此咱们通常写的class文件都是AppClassLoader加载的。那假如咱们写了一个类,咱们把这个类复制一份,放到ExtClassLoader目录下,那么类加载器会怎么加载呢?这就要提到类加载器的委托机制了。加密

类加载器的委托机制:当一个线程调用一个类的时候,首先用当前线程的类加载器去加载这个类,这个类加载器一开始不加载,会通知他的上一级类加载器去加载,等到了BootStrap加载器的时候,若是没有就再让调用的下级加载器去加载。若是都没有就报ClassNotFoundException异常。也能够直接指定类加载器去加载。spa

因此咱们能够本身定义一个类加载器,让这个让这个类加载器去加载咱们加密过的类。自定义类加载器须要继承ClassLoader类,而且重写findClass方法。

class MyClassLoader extends ClassLoader {
    private String path = null;

    // 设置自定义类加载器的目录
    public MyClassLoader(String path) {
        this.path = path;
    }

    /*
     * findClass和loadClass的区别?
     */
    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        try {           
            File f = new File(path, name.substring(name.lastIndexOf('.') + 1)
                    + ".class");
            FileInputStream fis = new FileInputStream(f);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int ch = 0;
            while ((ch = fis.read()) != -1) {
                bos.write(ch);
            }
            byte[] buf = bos.toByteArray();
            //解密.class
            buf = Z1Encrypt.crypt(buf, "decrypt");
            fis.close();
            bos.close();
            //根据字节码返回这个类
            return defineClass(name, buf, 0, buf.length);
        } catch (Exception e) {
            throw new ClassNotFoundException(name + " is not found!");
        }
    }
}
复制代码

接着咱们用咱们的自定义类加载器去解密加载咱们加密好的字节码文件

public static void main(String[] args) throws Exception {
        /*Z1Encrypt.readClass("F:\\WorkSpace\\classLoader\\DemoTemp.class");
        Z1Encrypt.encrypt();
        */
        MyClassLoader mcl = new MyClassLoader("F:\\WorkSpace\\classLoader\\");
        Class clazz  = mcl.findClass("DemoTemp");
        Method me = clazz.getMethod("say",null);                       
        Object obj = clazz.newInstance();
        me.invoke(obj, null);
    }
复制代码

后来成功的解密了字节码文件,并将其加载到内存中,并成功调用了相关的方法了。

以上是一个很是简单的代码加密,并自定义类加载器的方法,仅供学习其中思路和知识。

相关文章
相关标签/搜索