隨筆-348  評論-598  文章-0  trackbacks-0
          之前寫了一片文章介紹了應用內外的簽名比較(在Android系統(tǒng)外部和內部讀取Android應用的簽名),發(fā)現(xiàn)在某些機器上,讀取安裝包的簽名有的時候為空,所以找了其他辦法。
              public static String getPathWithPackageName(Context paramContext,
                      String paramString) {
                  PackageManager pm = paramContext.getPackageManager();
                  try {
                      PackageInfo pi = pm.getPackageInfo(paramString, 1);
                      System.out.println(pi.applicationInfo.publicSourceDir);
                      return pi.applicationInfo.publicSourceDir;
                  } catch (PackageManager.NameNotFoundException localNameNotFoundException) {

                  }
                  return null;
              }

              public static String getFileSignatureMd5(String filePath) {
                  try {
                      JarFile jarFile = new JarFile(filePath);
                      JarEntry jarEntry = jarFile.getJarEntry("AndroidManifest.xml");
                      if (jarEntry != null) {
                          try {
                              Certificate[] certs = jarEntry.getCertificates();
                              byte[] certBytes = certs[0].getEncoded();
                              String main = StringUtilities
                                      .bytesToHexString(CryptoUtilities
                                              .encryptByMD5(certBytes));
                              System.out.println("new to Char " + main.length() + " apk:"
                                      + main);
                              return main;
                          } catch (CertificateEncodingException localCertificateEncodingException) {

                          }
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  return null;
              }

              Handler handler3;

              /** Called when the activity is first created. */
              @Override
              public void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.main);
                  String installedPath = getPathWithPackageName(this,
                          "cn.emagsoftware.gamehall");
                  System.out.println("installedPath=" + installedPath);
                  String installedSig = getFileSignatureMd5(installedPath);
                  System.out.println("installedSig=" + installedSig);

                  String apk = "/sdcard/1.apk";
                  String remoteSig = getFileSignatureMd5(apk);
                  System.out.println("remoteSig=" + remoteSig);
                  System.out.println(installedSig.equals(remoteSig));
              }










          ---------------------------------------------------------
          專注移動開發(fā)

          Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
          posted on 2012-07-04 21:18 TiGERTiAN 閱讀(1674) 評論(0)  編輯  收藏 所屬分類: Android
          主站蜘蛛池模板: 平阴县| 双辽市| 东乡族自治县| 来凤县| 乐业县| 安乡县| 平潭县| 都匀市| 阜平县| 大足县| 盖州市| 岳阳市| 新闻| 乌恰县| 历史| 华池县| 广南县| 昭觉县| 会泽县| 宝兴县| 许昌县| 齐齐哈尔市| 如皋市| 克拉玛依市| 阿克苏市| 德化县| 大理市| 龙南县| 鲜城| 汶上县| 甘谷县| 郴州市| 玉门市| 青田县| 广州市| 雷州市| 威远县| 托克逊县| 兴隆县| 柳河县| 新民市|