之前写了一片文章介绍了应用内外的签名比较(
在Android系统外部和内部读取Android应用的签名),发现在某些机器上,读取安装包的签名有的时候为空,所以找了其他办法。
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));
}
---------------------------------------------------------
专注移动开发
Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
posted on 2012-07-04 21:18
TiGERTiAN 阅读(1655)
评论(0) 编辑 收藏 所属分类:
Android