Android获取apk签名信息

/**
 * 使用Java自带的API(主要用到的为JarFile,JarEntry,Certificate)进行获取,
 * 还有一种方法是使用系统隐藏的API PackageParser,通过反射来使用对应的API.  
 * 但是由于安卓系统的分裂版本过多,并且不同厂商进行的修改很多,
 * 依赖反射隐藏API的方法并不能保证兼容性和通用性,因此推荐使用JAVA自带API进行获取
 */
/**
 * 从APK中读取签名
 * @param file
 * @return
 * @throws IOException
 */
private static List<String> getSignaturesFromApk(File file) throws IOException {
    List<String> signatures=new ArrayList<String>();
    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;
}
/**
 * 加载签名
 * @param jarFile
 * @param je
 * @param readBuffer
 * @return
 */
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;
}

/**
 * 将签名转成转成可见字符串
 * @param sigBytes
 * @return
 */
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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值