简单的class文件加密解密类加载器使用。

/**
 * 加解密工具
 */
public class EncryptionUtils extends ClassLoader{
    /**统一加密/解密 密钥 **/
    private int password = 0xFF;

    private static String parentPath ="D://";

    /**
     * 加密方法
     * @param pathUrl 类名称
     */
    public void encryptionClass(String pathUrl){
        if(null==pathUrl || "".equals(pathUrl)){
            System.out.println("路径不能为空!");
            return;
        }
        String path =new File(pathUrl).getAbsolutePath();

        //获取文件
        File file = new File(path);
        if(!file.exists()){
            throw new NoClassDefFoundError("没有找到文件!");
        }
        //获取文件夹目录
        File awaitFileFolder = new File(file.getParent() + File.separator);
        //不存在则创建目录
        if(!awaitFileFolder.exists()){
            awaitFileFolder.mkdirs();
        }
        File encryptFolder = new File(awaitFileFolder.getParent() + File.separator );
        if (!encryptFolder.exists()) {
            encryptFolder.mkdirs();
        }
        //加密后文件指定存放目录
// String encryptedFile = file.getParent() + File.separator + File.separator + file.getName();
        String encryptedFile = parentPath + file.getName();
        try (
                FileInputStream fileInputStream = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(fileInputStream);
                FileOutputStream fileOutputStream = new FileOutputStream(encryptedFile);
                BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream)
        ) {
            // 加密
            int data;
            while ((data = bis.read()) != -1) {
                bos.write(data ^ password);
            }
            bos.flush();
        } catch (IOException e) {
            System.out.println("加密异常!");
            e.printStackTrace();
        }
        // 现在将原来未加密的文件删除
        // awaitFileFolder.delete();

//        // 先获取加密前文件绝对路径名,然后修改后缀,再创建File对象
//        File oldFile = new File(parentPath +file.getName()+ "hong");
//        // 删除未加密前的文件
 if (oldFile.exists()) {
 oldFile.delete();
 }
//        // 根据加密后的文件创建File对象
//        File newEncryptedFile = new File(encryptedFile);
//        // 将加密后的对象重命名,这时加密后的文件就把加密前的文件替换掉了,这就是为什么刚开始加密后的文件需要单独放的原因
//        newEncryptedFile.renameTo(oldFile);
//        // 删除之前的加密文件夹下面的加密文件
//        newEncryptedFile.getParentFile().delete();
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        //TODO: 把全路径文件加载到内存中
        byte[] bytes = decryptClass(name);
        int length = bytes ==null?0:bytes.length;
        return defineClass("org.jeecg.unit.JiamiClass.TestContent",bytes,0,length);
    }

    /**
     * 运行主类
     * @param args
     * @throws ClassNotFoundException
     */
    public static void main(String[] args) throws ClassNotFoundException {
        //获取项目路劲
        File file = new File(EncryptionUtils.class.getResource("/").getPath());
        //加解密工具
        EncryptionUtils encryptionUtils = new EncryptionUtils();
        //解析路劲
        encryptionUtils.encryptionClass(file.getPath()+"/org/jeecg/unit/JiamiClass/TestContent.class");
        //获取解析后的文件
        Class<?> c = encryptionUtils.loadClass(parentPath+"/TestContent.class");
        System.out.println(c.getClassLoader());
        System.out.println(c.getClassLoader().getParent());
        System.out.println(c.getClassLoader().getParent().getParent());
        System.out.println(c.getClassLoader().getParent().getParent().getParent());
        if(c != null){
            try {
                Object obj = c.newInstance();
                //获取方法
                Method method = c.getDeclaredMethod("hong");
                //通过反射调用Test类的say方法
                method.invoke(obj);
            } catch (InstantiationException | IllegalAccessException
                    | NoSuchMethodException
                    | SecurityException |
                    IllegalArgumentException |
                    InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 解密方法,解密后转成byte[]字节加载到内存使用.
     * @param pathUrl 需要解密的文件名
     */
    protected byte[] decryptClass(String pathUrl) {
        String path;
        if (!pathUrl.contains(".class")) {
            path = new File(pathUrl).getAbsolutePath();
        } else {
            path = pathUrl;
        }
        //获取待解密文件
        File decryptClassFile = new File(path);
        if (!decryptClassFile.exists()) {
            System.out.println("decryptClass() File:" + path + " not found!");
            return null;
        }
        byte[] result = null;
        BufferedInputStream bis = null;
        ByteArrayOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(decryptClassFile));
            bos = new ByteArrayOutputStream();
            // 解密
            int data;
            while ((data = bis.read()) != -1) {
                bos.write(data ^ password);
            }
            bos.flush();
            result = bos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 解密方法,解密后还原成文件.
     * @param pathUrl 需要解密的文件名
     */
    protected void decryptClassHuanyuan(String pathUrl) {
        String path;
        if (!pathUrl.contains(".class")) {
            path = new File(pathUrl).getAbsolutePath();
        } else {
            path = pathUrl;
        }
        //获取待解密文件
        File decryptClassFile = new File(path);
        if (!decryptClassFile.exists()) {
            System.out.println("decryptClass() File:" + path + " not found!");
        }else {
            byte[] result = null;
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            String encryptedFile = parentPath + "ThreadConfigjiexi.class";
            try {
                bis = new BufferedInputStream(new FileInputStream(decryptClassFile));

                FileOutputStream fileOutputStream = new FileOutputStream(encryptedFile);
                bos = new BufferedOutputStream(fileOutputStream);
                // 解密
                int data;
                while ((data = bis.read()) != -1) {
                    bos.write(data ^ password);
                }
                bos.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bis != null) {
                        bis.close();
                    }
                    if (bos != null) {
                        bos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值