DEX files, which are the compiled bytecode files that run on the Android OS. Essentially they are like the java bytecode, except they use a modified VM which is called “Dalvik” that was developed for the Android OS.
private static void calcSignature(byte bytes[]){MessageDigest md;try{md = MessageDigest.getInstance("SHA-1");}catch(NoSuchAlgorithmException ex){throw new RuntimeException(ex);}md.update(bytes, 32, bytes.length - 32);try{int amt = md.digest(bytes, 12, 20);if(amt != 20)throw new RuntimeException((new StringBuilder()).append("unexpected digest write:").append(amt).append("bytes").toString());}catch(DigestException ex){throw new RuntimeException(ex);}}private static void calcChecksum(byte bytes[]){Adler32 a32 = new Adler32();a32.update(bytes, 12, bytes.length - 12);int sum = (int)a32.getValue();bytes[8] = (byte)sum;bytes[9] = (byte)(sum >> 8);bytes[10] = (byte)(sum >> 16);bytes[11] = (byte)(sum >> 24);}
Ah hah! Now we know how to calculate the signature and the checksum. Essentially is reads in all the bytes of the program, and it disreguards the first 32 bytes and calculates the signature using SHA-1. Then it calculates the checksum disreguarding the first 12 bytes (so it includes the signature). Excellent, lets drop this code into our own program so we can recalculate those values;
现在我们知道如何计算签名跟校验和了。本质上是读取程序所有的字节数,它disreguards 最先的32字节和计算签名使用“SHA-1”,它计算这个校验和disreguarding 最先的12字节(所以它包含签名),好极了~,把它加进我们自己的程序中,我们也可以重新计算这些值了。
/** ReDEX.class* Coded: Timothy Strazzere** 10/29/2008** Pass a .dex file as an argument and it will output the current* checksum and signature that is in the file, then it will output* the checksum and signature as it calculates them.**/import java.security.*;import java.util.zip.Adler32;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;public class ReDEX {public static void main(String[] args) {if (args.length == 1) {try {File file = new File(args[0]);byte[] barr = null;barr = getBytesFromFile(file);System.out.print(:Original Checksum: ");for(int i = 8; i<12; i+=4)System.out.printf("0x%02X%02X%02X%02X ", barr[i+3], barr[i+2], barr[i+1], barr[i]);System.out.print("\nOriginal Signature: 0x");for(int i = 12; i<32; i+=4)System.out.printf"%02X%02X%02X%02X ", barr[i], barr[i+1], barr[i+2], barr[i+3]);calcSignature(barr);calcChecksum(barr);System.out.print("\n\nNew Checksum: ");for(int i = 8; i<12; i+=4)System.out.printf("0x%02X%02X%02X%02X ", barr[i+3], barr[i+2], barr[i+1], barr[i]);System.out.print("\nNew Signature: 0x");for(int i = 12; i<32; i+=4)System.out.printf("%02X%02X%02X%02X ", barr[i], barr[i+1], barr[i+2], barr[i+3]);}catch (Exception e) {System.err.println("File input error");}}elseSystem.out.println("Invalid parameters");}public static byte[] getBytesFromFile(File file) throws IOException {InputStream is = new FileInputStream(file);// Get the size of the filelong length = file.length();if (length > Integer.MAX_VALUE) {// File is too large}// Create the byte array to hold the databyte[] bytes = new byte[(int)length];// Read in the bytesint offset = 0;int numRead = 0;while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {offset += numRead;}// Ensure all the bytes have been read inif (offset < bytes.length) {throw new IOException("Could not completely read file "+file.getName());}// Close the input stream and return bytesis.close();return bytes;}private static void calcSignature(byte bytes[]){MessageDigest md;try{md = MessageDigest.getInstance("SHA-1");}catch(NoSuchAlgorithmException ex){throw new RuntimeException(ex);}md.update(bytes, 32, bytes.length - 32);try{int amt = md.digest(bytes, 12, 20);if(amt != 20)throw new RuntimeException((new StringBuilder()).append("unexpected digest write:").append(amt).append("bytes").toString());}catch(DigestException ex){throw new RuntimeException(ex);}}private static void calcChecksum(byte bytes[]){Adler32 a32 = new Adler32();a32.update(bytes, 12, bytes.length - 12);int sum = (int)a32.getValue();bytes[8] = (byte)sum;bytes[9] = (byte)(sum >> 8);bytes[10] = (byte)(sum >> 16);bytes[11] = (byte)(sum >> 24);}
Excellent, now I can easily recalculate the signature and the checksum of the dex files. Note that the checksum is actually in little-endian so you need to reverse it when entering it in a hex editor.
好极了,现我我能很容易的重新计算dex文件 校验和跟签名,注意校验和实际是低字节,所以你要扭转时,要用16进制的编辑器。
So, now the Android OS will allow you to install this dex file, after signing it in the correct package (apk). Though as of right now the program still crashes upon launching the file… Hhhmmmmm we’ll have to look into this more I guess.
所以,现在android Os 也能让你安装这个dex file,签名后打包,虽然到目前为止,程序一直崩溃。。。。