首页
社区
课程
招聘
[旧帖] [分享]Dex文件头修复 -- java源码 0.00雪花
发表于: 2015-8-18 10:59 1481

[旧帖] [分享]Dex文件头修复 -- java源码 0.00雪花

2015-8-18 10:59
1481
由于需要在android项目中修改dex文件,所以自己实现了一个dex文件头修复。分享一下。代码如下,可以直接拿来用。比较简单所以就不注释了。


public class FixDexHeaderUtil {

  public static void fix(String file) {
    byte[] fBytes = readFile(file);
    fix(fBytes);
    saveFile(fBytes, file);
  }

  private static byte[] readFile(String file) {
    FileInputStream fis = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
      fis = new FileInputStream(file);
      if (fis != null && bos != null) {
        int len = -1;
        byte[] buf = new byte[512];
        while ((len = fis.read(buf)) != -1) {
          bos.write(buf, 0, len);
          bos.flush();
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (fis != null) {
        try {
          fis.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    byte[] fBytes = bos.toByteArray();
    if (bos != null) {
      try {
        bos.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return fBytes;
  }

  private static void saveFile(byte[] fBytes, String file) {
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(file);
      fos.write(fBytes);
      fos.flush();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (fos != null) {
        try {
          fos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }

  private static void fix(byte[] fBytes) {
    fixSha1Signature(fBytes);
    fixChecksum(fBytes);
  }

  private static void fixSha1Signature(byte[] fBytes) {
    MessageDigest sha1 = null;
    try {
      sha1 = MessageDigest.getInstance("SHA1");
      sha1.update(fBytes, 32, fBytes.length - 32);
      byte[] hashBytes = sha1.digest();
      for (int i = 0; i < hashBytes.length; i++) {
        fBytes[12 + i] = hashBytes[i];
      }
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    }
  }

  private static void fixChecksum(byte[] fBytes) {
    Adler32 al = new Adler32();
    al.update(fBytes, 12, fBytes.length - 12);
    int sum = (int) al.getValue();
    byte[] result = new byte[4];
    result[0] = (byte) sum;
    result[1] = (byte) (sum >> 8);
    result[2] = (byte) (sum >> 16);
    result[3] = (byte) (sum >> 24);
    for (int i = 0; i < result.length; i++) {
      fBytes[8 + i] = result[i];
    }
  }
}


[课程]Linux pwn 探索篇!

收藏
免费 0
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回
//