dex、apk完整性校验

对Dex进行完整性的检查,可通过CRC,或者Hash值。可将校验值放到String资源文件里,或者放到服务器中。

  1. 在代码中完成校验值对比逻辑,此部分代码后续不能再改变,否则CRC值会发生变化;
  2. 从生成的APK文件中提取出classes.dex文件,计算CRC值,或Hash值,
  3. 将计算出的值放入strings.xml文件中
string apkPath=this.getPackageCodePath();

Long dexCRC=Long.parseLong(this.getString(R.string.dex_crc));

try{
     ZipFile zipfile=new ZipFile(apkPath);

     ZipEntry dexentry=zipfile.getEntry("classes.dex");
         
     if(dexentry.getCrc()!=dexCRC){//代码逻辑中,进行校验
          System.out.println("Dex has been modified!");
     }else
     ...
破解方法:逻辑判断的true/false容易更改



对APK的完整性校验:Hash值放在服务端比较合适。

MessageDigest msgDigest=null;

try{
     msgDigest= MessageDigest.getInstance("md5");
     byte[] bytes=new byte[8192];
     int byteCount;

     FileInputStream fis=null;
     fis =new FileInputStream(new File(apkpath));
     
     while((byteCount=fis.read(bytes))>0){
          msgDigest.update(bytes,0,byteCount);
          BinInteger bi=new BigInteger(1,msgDigest.digest());
          String md5 =bi.toString(16);
          fis.close();
     
     ...
}






posted @ 2014-08-30 10:55  徐小鱼  阅读(696)  评论(0编辑  收藏  举报