您的位置:首页 > 移动开发 > Android开发

android 读取apk签名信息

2013-08-07 14:33 274 查看
某些时候需要获取某个特定的apk(已安装或者未安装)的签名信息,如程序自检测,可信赖的第三方检测(应用市场),系统限定安装  

对此,有两种实现方法  

可以使用Java自带的API(主要用到的为JarFile,JarEntry,Certificate)进行获取,还有一种方法是使用系统隐藏的API PackageParser,通过反射来使用对应的API.  

但是由于安卓系统的分裂版本过多,并且不同厂商进行的修改很多,依赖反射隐藏API的方法并不能保证兼容性和通用性,因此推荐使用JAVA自带API进行获取:  

  

      

    private static List getSignaturesFromApk(File file) throws IOException {  

        List signatures=new ArrayList();  

        JarFile jarFile=new JarFile(file);  

        try {  

            JarEntry je=jarFile.getJarEntry("AndroidManifest.xml");  

            byte[] readBuffer=new byte[8192];  

            Certificate[] certs=loadCertificates(jarFile, je, readBuffer);  

            if(certs != null) {  

                for(Certificate c: certs) {  

                    String sig=toCharsString(c.getEncoded());  

                    signatures.add(sig);  

                }  

            }  

        } catch(Exception ex) {  

        }  

        return signatures;  

    }  

 

[java] view plaincopy

      

    private static 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 je != null ? je.getCertificates() : null;  

        } catch(IOException e) {  

        }  

        return null;  

    }  

  

  

  

  

    private static String toCharsString(byte[] sigBytes) {  

        byte[] sig=sigBytes;  

        final int N=sig.length;  

        final int N2=N * 2;  

        char[] text=new char[N2];  

        for(int j=0; j < N; j++) {  

            byte v=sig[j];  

            int d=(v >> 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 new String(text);  

    }  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: