第七下载是国内最新、最齐、最安全的软件下载基地!

关于我们最近更新热门排行

首页 入库 软件 游戏 安卓 MAC 文章 专题

新闻资讯软件应用软件教程 电脑百科编程开发操作系统游戏娱乐游戏攻略手机相关游戏视频
当前位置:首页 ›› 软件教程 ›› apk文件加壳的源代码

apk文件加壳的源代码

2014-12-04 09:48   作者:佚名   来源:本站整理   浏览:225   评论:1  

对APK进行加密加固,可以防止他人进行反编译,逆向分析,获取源码,注入广告代码,恶意代码,再进行二次打包。但是加密并不是一个轻松的过程,而且需要大量的代码支持,这些对于新手们来说可能过于难了,于是小编在这里准备了一篇详细的教程,具体如下。

代码实现

本文的代码实现了对整个APK包的加壳处理。加壳程序不会对源程序有任何的影响。本程序基于Android2.3代码实现,因为牵扯到系统代码的反射修改,本程序不保证在其它android版本正常工作,大家可以根据实现原理,自行实现对其它Android版本的兼容性开发。

1、加壳程序流程及代码实现

1、加密源程序APK为解壳数据

2、把解壳数据写入解壳程序DEX文件末尾,并在文件尾部添加解壳数据的大小。

3、修改解壳程序DEX头中checksum、signature 和file_size头信息。

package com.android.dexshell;  
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 DexShellTool {  
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
        try {  
 	   File payloadSrcFile = new File("g:/payload.apk");  
 	   File unShellDexFile = new File("g:/unshell.dex");  
 	   byte[] payloadArray = encrpt(readFileBytes(payloadSrcFile));  
 	   byte[] unShellDexArray = readFileBytes(unShellDexFile);  
 	   int payloadLen = payloadArray.length;  
 	   int unShellDexLen = unShellDexArray.length;  
 	   int totalLen = payloadLen + unShellDexLen +4;  
 	   byte[] newdex = new byte[totalLen];  
 	   //添加解壳代码  
 	   System.arraycopy(unShellDexArray, 0, newdex, 0, unShellDexLen);  
 	   //添加加密后的解壳数据  
 	   System.arraycopy(payloadArray, 0, newdex, unShellDexLen,  
 		   payloadLen);  
 	   //添加解壳数据长度  
 	   System.arraycopy(intToByte(payloadLen), 0, newdex, totalLen-4, 4);  
 		       //修改DEX file size文件头  
 	   fixFileSizeHeader(newdex);  
 	   //修改DEX SHA1 文件头  
 	   fixSHA1Header(newdex);  
 	   //修改DEX CheckSum文件头  
 	   fixCheckSumHeader(newdex);  
  
  
 	   String str = "g:/classes.dex";  
 	   File file = new File(str);  
 	   if (!file.exists()) {  
 	       file.createNewFile();  
 	   }  
 	     
 	   FileOutputStream localFileOutputStream = new FileOutputStream(str);  
 	   localFileOutputStream.write(newdex);  
 	   localFileOutputStream.flush();  
 	   localFileOutputStream.close();  
  
  
        } catch (Exception e) {  
 	   // TODO Auto-generated catch block  
 	   e.printStackTrace();  
        }  
    }  
      
    //直接返回数据,读者可以添加自己加密方法  
    private static byte[] encrpt(byte[] srcdata){  
        return srcdata;  
    }  
  
  
    private static void fixCheckSumHeader(byte[] dexBytes) {  
        Adler32 adler = new Adler32();  
        adler.update(dexBytes, 12, dexBytes.length - 12);  
        long value = adler.getValue();  
        int va = (int) value;  
        byte[] newcs = intToByte(va);  
        byte[] recs = new byte[4];  
        for (int i = 0; i = 0; i--) {  
 	   b[i] = (byte) (number % 256);  
 	   number >>= 8;  
        }  
        return b;  
    }  
  
  
    private static void fixSHA1Header(byte[] dexBytes)  
 	   throws NoSuchAlgorithmException {  
        MessageDigest md = MessageDigest.getInstance("SHA-1");  
        md.update(dexBytes, 32, dexBytes.length - 32);  
        byte[] newdt = md.digest();  
        System.arraycopy(newdt, 0, dexBytes, 12, 20);  
        String hexstr = "";  
        for (int i = 0; i < newdt.length; i++) {  
 	   hexstr += Integer.toString((newdt[i] & 0xff) + 0x100, 16)  
 		   .substring(1);  
        }  
        System.out.println(hexstr);  
    }  
  
  
    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);  
    }  
  
  
    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();  
 	   }  
        }  
    }  
  
  
}
Tags:责任编辑:xinxl
    1. 加壳脱壳软件加壳脱壳工具

      第七下载提供加壳脱壳工具下载,软件脱壳,顾名思义,就是对软件加壳的逆操作,把软件上存在的壳去掉。在一些计算机软件里也有一段专门负责保护软件不被非法修改或反编译的程序。加壳的全称应该是可执行程序资源压缩,...

    软件评论

    请自觉遵守互联网相关政策法规,评论内容只代表网友观点,与本站立场无关!

        登录   注册
    猜你喜欢