apk加固原理之dex加密

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;
    }

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值