码迷,mamicode.com
首页 > 移动开发 > 详细

获取 Android app的签名

时间:2016-05-09 15:53:42      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:

可以利用Java的JarFile ,JarEntry,Certificate  来获取android app的签名信息

 

public class Do {

    private static final char[] wJ = "0123456789abcdef".toCharArray();

    /**
     * 从APK中读取签名
     * 
     * @param file
     * @return
     * @throws IOException
     */
    public String getSignaturesFromApk(File file) throws IOException {
        JarFile jarFile = new JarFile(file);
        try {
            JarEntry je = jarFile.getJarEntry("AndroidManifest.xml");
            byte[] readBuffer = new byte[8192];
            Certificate[] certs = loadCertificates(jarFile, je, readBuffer);
            jarFile.close();
            if (certs != null) {
                for (Certificate c : certs) {
                    byte buffer[] = c.getEncoded();
                    return k(j(buffer));
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }finally{
            jarFile.close();
        }
        return null;
    }

    private String k(byte[] paramArrayOfByte) {
        StringBuilder localStringBuilder = new StringBuilder(
                3 * paramArrayOfByte.length);
        int i = paramArrayOfByte.length;
        for (int j = 0;; j++) {
            if (j >= i)
                return localStringBuilder.toString().toUpperCase();
            int k = 0xFF & paramArrayOfByte[j];
            localStringBuilder.append(wJ[(k >> 4)]);
            localStringBuilder.append(wJ[(k & 0xF)]);
        }
    }

    public byte[] j(byte[] paramArrayOfByte) {
        try {
            MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
            localMessageDigest.update(paramArrayOfByte);
            byte[] arrayOfByte = localMessageDigest.digest();
            return arrayOfByte;
        } catch (NoSuchAlgorithmException localNoSuchAlgorithmException) {
            localNoSuchAlgorithmException.printStackTrace();
        }
        return null;
    }

    /**
     * 加载签名
     * 
     * @param jarFile
     * @param je
     * @param readBuffer
     * @return
     */
    private Certificate[] loadCertificates(JarFile jarFile, JarEntry je,
            byte[] readBuffer) {
        InputStream is = null;
        try {
            is = jarFile.getInputStream(je);
            while (is.read(readBuffer, 0, readBuffer.length) != -1) {
            }
//            is.close();
            return je != null ? je.getCertificates() : null;
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

}

 

获取 Android app的签名

标签:

原文地址:http://www.cnblogs.com/phyxis/p/5474023.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!