当前位置: 首页 > news >正文

Java使用拷贝asset文件,解密,并用DexclassLoader加载执行

//asset中加密的apk文件重命名为index.html,拷贝到私有目录
//解密
//加载,执行apk中的方法
public static void handleByJava(Context context){File copyedFile = new File(context.getFilesDir().getAbsolutePath() + "/" + "main.html");FileUtil.copyAssetFile(context, "index.html", copyedFile.getAbsolutePath());FileUtil.customDecryptFile(copyedFile, context.getFilesDir().toString(), "home.html", "android");String apkPath = new File(context.getFilesDir()+"/"+"home.html").getAbsolutePath();DexClassLoader classLoader = new DexClassLoader(apkPath, context.getFilesDir().getAbsolutePath(), null, context.getClassLoader());if(new File(apkPath).exists()){Log.e("xxx",apkPath+"存在");}try {// 加载指定的类Class<?> clazz = classLoader.loadClass("com.example.myapplication.Heave");// 创建类的实例Object  heave = clazz.newInstance();Method method = clazz.getMethod("initAF",android.content.Context.class);method.invoke(heave,context);} catch (Exception e) {e.printStackTrace();}}
//拷贝asset文件到私有目录public static void copyAssetFile(Context context, String assetFileName, String destinationPath) {AssetManager assetManager = context.getAssets();InputStream inputStream = null;OutputStream outputStream = null;try {inputStream = assetManager.open(assetFileName);File destinationFile = new File(destinationPath);if (!destinationFile.exists()) {destinationFile.createNewFile();}outputStream = new FileOutputStream(destinationFile);byte[] buffer = new byte[1024];int read;while ((read = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, read);}outputStream.flush();} catch (IOException e) {e.printStackTrace();} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}if (outputStream != null) {try {outputStream.close();} catch (IOException e) {e.printStackTrace();}}}}
public static File  customEncryptFile(File sourceFile, String dir, String toFileName, String secretKey){try {// 创建加密后的文件File encryptFile = new File(dir, toFileName);FileInputStream inputStream = new FileInputStream(sourceFile);// 根据文件创建输出流FileOutputStream outputStream = new FileOutputStream(encryptFile);// 创建缓存字节数组byte[] buffer = new byte[1024 * 2];// 读取int len;// 读取加密并写入文件while ((len = inputStream.read(buffer)) != -1) {outputStream.write(buffer,0 , len);outputStream.write(secretKey.getBytes());outputStream.flush();}closeStream(outputStream);return encryptFile;} catch (Exception e) {handleException(e);}return null;}public static File customDecryptFile(File sourceFile, String dir, String toFileName, String secretKey) {try {File decryptFile = new File(dir, toFileName);if(decryptFile.exists()){decryptFile.delete();}FileInputStream inputStream = new FileInputStream(sourceFile);// 获取解密输出流FileOutputStream outputStream = new FileOutputStream(decryptFile);int length= secretKey.getBytes().length;byte[] buffer = new byte[1024 * 2+ length];int len;while ((len = inputStream.read(buffer)) >= 0) {outputStream.write(buffer,0 , len-length);outputStream.flush();}outputStream.close();closeStream(inputStream);return decryptFile;} catch (IOException e) {handleException(e);}return null;}private static void closeStream(Closeable closeable) {try {if (closeable != null) closeable.close();} catch (Exception e) {handleException(e);}}private static void handleException(Exception e) {e.printStackTrace();Log.e(TAG, TAG + e);}

http://www.lryc.cn/news/433668.html

相关文章:

  • 【AcWing】861. 二分图的最大匹配(匈牙利算法)
  • 经验笔记:JSP(JavaServer Pages)
  • 【零基础必看的数据库教程】——SQL WHERE 子句
  • vscode docker debug python
  • 【Kubernetes】常见面试题汇总(四)
  • MATLAB基础语法知识
  • PopupInner源码分析 -- ant-design-vue系列
  • Maven 的 pom.xml 文件中<dependency> 元素及其各个参数的解释
  • 【信创】Linux终端禁用USB存储 _ 统信 _ 麒麟 _ 方德
  • 开放API接口时要注意的安全处理总结
  • FastGPT自定义插件的icon
  • SprinBoot+Vue旅游网站的设计与实现
  • 代码随想录刷题day27丨455.分发饼干 ,376. 摆动序列 ,53. 最大子序和
  • Detect It Easy
  • c++开关灯
  • DevOps实现CI/CD实战(六)- Jenkins集成k8s
  • 张雪峰:物联网行业迎高光时刻!如何选择?我们诚聘销售工程师!
  • 利用多文件编程实现顺序表的创建,判满,插入,输出
  • 百度快照劫持之JS劫持诊断与恢复一例
  • 深入探讨Go语言中的切片与数组操作
  • 【WPS Excel】复制表格时,提示“图片太大,超过部份将被截去“ 问题
  • 驱动(RK3588S)第九课时:多节点驱动与函数接口
  • Linux系统下配置MySQL
  • 信捷 XD PLC POU编程之FB
  • 终于有人把云计算、大数据和人工智能讲明白了!
  • 【编程底层思考】详解Java内存模型(JMM)原理及其作用
  • Docker的基本概念和优势
  • 数据结构————内核链表
  • 使用API接口获取某宝商品数据详情
  • 用Python实现时间序列模型实战——Day 15: 时间序列模型的选择与组合