微信开发问题:
博客地址:
获取未安装的apk的签名信息:
Android环境下的签名信息获取
private String showUninstallAPKSignatures(String apkPath) { String PATH_PackageParser = "android.content.pm.PackageParser"; try { // apk包的文件路径 // 这是一个Package 解释器, 是隐藏的 // 构造函数的参数只有一个, apk文件的路径 // PackageParser packageParser = new PackageParser(apkPath); Class pkgParserCls = Class.forName(PATH_PackageParser); Class[] typeArgs = new Class[1]; typeArgs[0] = String.class; Constructor pkgParserCt = pkgParserCls.getConstructor(typeArgs); Object[] valueArgs = new Object[1]; valueArgs[0] = apkPath; Object pkgParser = pkgParserCt.newInstance(valueArgs); // 这个是与显示有关的, 里面涉及到一些像素显示等等, 我们使用默认的情况 DisplayMetrics metrics = new DisplayMetrics(); metrics.setToDefaults(); // PackageParser.Package mPkgInfo = packageParser.parsePackage(new // File(apkPath), apkPath, // metrics, 0); typeArgs = new Class[4]; typeArgs[0] = File.class; typeArgs[1] = String.class; typeArgs[2] = DisplayMetrics.class; typeArgs[3] = Integer.TYPE; Method pkgParser_parsePackageMtd = pkgParserCls.getDeclaredMethod("parsePackage", typeArgs); valueArgs = new Object[4]; valueArgs[0] = new File(apkPath); valueArgs[1] = apkPath; valueArgs[2] = metrics; valueArgs[3] = PackageManager.GET_SIGNATURES; Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(pkgParser, valueArgs); typeArgs = new Class[2]; typeArgs[0] = pkgParserPkg.getClass(); typeArgs[1] = Integer.TYPE; Method pkgParser_collectCertificatesMtd = pkgParserCls.getDeclaredMethod("collectCertificates", typeArgs); valueArgs = new Object[2]; valueArgs[0] = pkgParserPkg; valueArgs[1] = PackageManager.GET_SIGNATURES; pkgParser_collectCertificatesMtd.invoke(pkgParser, valueArgs); // 应用程序信息包, 这个公开的, 不过有些函数, 变量没公开 Field packageInfoFld = pkgParserPkg.getClass().getDeclaredField("mSignatures"); Signature[] info = (Signature[]) packageInfoFld.get(pkgParserPkg); return info[0].toCharsString(); } catch (Exception e) { e.printStackTrace(); } return null; }
获取已经安装的应用的签名信息
private String getSign(Context context) { PackageManager pm = context.getPackageManager(); Listapps = pm.getInstalledPackages(PackageManager.GET_SIGNATURES); Iterator iter = apps.iterator(); while(iter.hasNext()) { PackageInfo packageinfo = iter.next(); String packageName = packageinfo.packageName; if (packageName.equals(instance.getPackageName())) { MediaApplication.logD(DownloadApk.class, packageinfo.signatures[0].toCharsString()); return packageinfo.signatures[0].toCharsString(); } } return null; }
获取已安装应用的MD5指纹信息
public void getSingInfo() { try { PackageInfo packageInfo = getPackageManager().getPackageInfo("com.sina,weibo", PackageManager.GET_SIGNATURES); Signature[] signs = packageInfo.signatures; Signature sign = signs[0]; parseSignature(sign.toByteArray()); } catch (Exception e) { e.printStackTrace(); }}public void parseSignature(byte[] signature) { try { CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(signature)); String pubKey = cert.getPublicKey().toString(); String signNumber = cert.getSerialNumber().toString(); System.out.println("signName:" + cert.getSigAlgName()); System.out.println("pubKey:" + pubKey); System.out.println("signNumber:" + signNumber); System.out.println("subjectDN:"+cert.getSubjectDN().toString()); } catch (CertificateException e) { e.printStackTrace(); }}
非Android环境下的签名信息获取
private static char[] toChars(byte[] mSignature) { byte[] sig = mSignature; final int N = sig.length; final int N2 = N*2; char[] text = new char[N2]; for(int j=0;j>4)&0xf; text[j*2] = (char)(d >= 10 ? ('a' + d - 10) : ('0' + d)); d = v&0xf; text[j*2+1] = (char)(d >= 10 ? ('a' + d - 10) : ('0' + d)); } return text; } private static java.security.cert.Certificate[] loadCertificates(JarFile jarFile, JarEntry je, byte[] readBuffer) { try { InputStream is = jarFile.getInputStream(je); while(is.read(readBuffer,0,readBuffer.length)!=-1) { } is.close(); return (java.security.cert.Certificate[])(je!=null?je.getCertificates():null); } catch (Exception e) { e.printStackTrace(); System.err.println("Exception reading "+je.getName()+" in "+jarFile.getName()+": "+e); } return null; }public static String getApkSignInfo(String apkFilePath){ byte[] readBuffer = new byte[8192]; java.security.cert.Certificate[] certs = null; try{ JarFile jarFile = new JarFile(apkFilePath); Enumeration entries = jarFile.entries(); while(entries.hasMoreElements()){ JarEntry je = (JarEntry)entries.nextElement(); if(je.isDirectory()){ continue; } if(je.getName().startsWith("META-INF/")){ continue; } java.security.cert.Certificate[] localCerts = loadCertificates(jarFile,je,readBuffer); System.out.println("File " + apkFilePath + " entry " + je.getName()+ ": certs=" + certs + " ("+ (certs != null ? certs.length : 0) + ")"); if (certs == null) { certs = localCerts; }else{ for(int i=0; i