apk加固原理之dex加密
来源:互联网 发布:类似于dropbox的软件 编辑:程序博客网 时间:2024/05/22 17:11
apk加固原理之dex加密
原理其实不复杂,加固其实就是加密dex文件,防止dex专程jar 被阅读。后面才是热部署解密dex;
代码如下,注释有解释:
import android.os.Environment;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.zip.Adler32;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mergeDex(); } private void mergeDex() { File forceApk=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/zzz/force.apk"); File unShellDex=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/zzz/classes.dex"); File mergeDex = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/zzz/merge.dex"); try { byte[] apkBytes = encrpt(readFileBytes(forceApk)); byte[] dexBytes = readFileBytes(unShellDex); int apkLen = apkBytes.length; int dexLen = dexBytes.length; int totalLen = apkLen+dexLen+4; byte[] newBytes=new byte[totalLen]; System.arraycopy(dexBytes,0,newBytes,0,dexLen); System.arraycopy(apkBytes,0,newBytes,dexLen,apkLen); System.arraycopy(intToByte(apkLen),0,newBytes,totalLen-4,4); fixFileSizeHeader(newBytes); fixSHA1Header(newBytes); fixCheckSumHeader(newBytes); FileOutputStream fileOutputStream=new FileOutputStream(mergeDex); fileOutputStream.write(newBytes); fileOutputStream.flush(); fileOutputStream.close(); } catch (Exception e) { Log.i("mytool", e.getMessage()+""); e.printStackTrace(); } } /** * 修改dex头,CheckSum 校验码 * * @param dexBytes */ private static void fixCheckSumHeader(byte[] dexBytes) { Adler32 adler = new Adler32(); adler.update(dexBytes, 12, dexBytes.length - 12);// 从12到文件末尾计算校验码 long value = adler.getValue(); int va = (int) value; byte[] newcs = intToByte(va); // 高位在前,低位在前掉个个 byte[] recs = new byte[4]; for (int i = 0; i < 4; i++) { recs[i] = newcs[newcs.length - 1 - i]; System.out.println(Integer.toHexString(newcs[i])); } System.arraycopy(recs, 0, dexBytes, 8, 4);// 效验码赋值(8-11) System.out.println(Long.toHexString(value)); System.out.println(); } /** * 修改dex头 sha1值 * * @param dexBytes */ private static void fixSHA1Header(byte[] dexBytes) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("SHA-1"); md.update(dexBytes, 32, dexBytes.length - 32);// 从32为到结束计算sha--1 byte[] newdt = md.digest(); System.arraycopy(newdt, 0, dexBytes, 12, 20);// 修改sha-1值(12-31) // 输出sha-1值,可有可无 String hexstr = ""; for (int i = 0; i < newdt.length; i++) { hexstr += Integer.toString((newdt[i] & 0xff) + 0x100, 16) .substring(1); } System.out.println(hexstr); } /** * 修改dex头 file_size值 * * @param dexBytes */ private static void fixFileSizeHeader(byte[] dexBytes) { // 新文件长度 byte[] newfs = intToByte(dexBytes.length); System.out.println(Integer.toHexString(dexBytes.length)); byte[] refs = new byte[4]; // 高位在前,低位在前掉个个 for (int i = 0; i < 4; i++) { refs[i] = newfs[newfs.length - 1 - i]; System.out.println(Integer.toHexString(newfs[i])); } System.arraycopy(refs, 0, dexBytes, 32, 4);// 修改(32-35) } /** * int 转byte[] */ public static byte[] intToByte(int number) { byte[] b = new byte[4]; for (int i = 3; i >= 0; i--) { b[i] = (byte) (number % 256); number >>= 8; } return b; } /** * 以二进制读出文件内容 * * @param file * @return */ @SuppressWarnings("resource") private static byte[] readFileBytes(File file) throws IOException { byte[] arrayOfByte = new byte[1024]; ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream(); FileInputStream fis = new FileInputStream(file); while (true) { int i = fis.read(arrayOfByte); if (i != -1) { localByteArrayOutputStream.write(arrayOfByte, 0, i); } else { return localByteArrayOutputStream.toByteArray(); } } } /** * 直接返回数据,读者可以添加自己加密方法 */ private static byte[] encrpt(byte[] srcdata) { for (int i = 0; i < srcdata.length; i++) { srcdata[i] = (byte) (0xFF ^ srcdata[i]); } return srcdata; }}
阅读全文
0 0
- apk加固原理之dex加密
- android加固系列—6.仿爱加密等第三方加固平台之动态加载dex防止apk被反编译
- apk加固原理之解密壳工程
- APK 加固原理
- Apk加固原理解析
- apk加固之360加固
- Dex加固总结之腾讯加固
- Android中apk加固完善篇之内存加载dex方案实现原理(不落地方式加载)
- Android中apk加固完善篇之内存加载dex方案实现原理(不落地方式加载)
- Android中apk加固完善篇之内存加载dex方案实现原理(不落地方式加载)
- Android Apk加固的初步实现思路(dex整体加固)
- andorid apk加固(加壳)原理
- Android中的Apk的加固实现原理
- android APK加固篇-2.动态加载dex及dex的方法的调用
- apkprotect(免费android代码混淆、加密保护工具) Android中的Apk的加固(加壳)原理解析和实现
- Apk加固
- APK加固
- 安卓Apk优化之加固
- spring boot 热部署配置
- lis 最长上升子序列
- Qt中的文件操作
- Spring配置bean
- 菜鸟先飞之JAVA_static关键字
- apk加固原理之dex加密
- mybatis(4)--完成基本的增删改查操作
- 正式开始学习Yii框架
- 知识点9:常见的排序算法–插入排序
- wpa_supplicant/wpa_cli
- hashmap知识点
- JS学习(2)----变量
- MySQL 数据库常用命令小结
- 计算机基础--部分