-
-
[旧帖] [分享]Dex文件头修复 -- java源码 0.00雪花
-
发表于: 2015-8-18 10:59 1510
-
由于需要在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]; } } }
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!
赞赏
看原图
赞赏
雪币:
留言: