zoukankan      html  css  js  c++  java
  • 获取 Android app的签名

    可以利用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;
        }
    
    }
  • 相关阅读:
    poj 1753 Flip Game
    SDIBT 2345 (3.2.1 Factorials 阶乘)
    HDU 1176 免费馅饼
    HDU 1058 Humble Numbers
    HDU 1003 MAXSUM(最大子序列和)
    HDU1864 最大报销额
    HDU 1114 Piggy-Bank(完全背包)
    POJ 3624 Charm Bracelet
    处理textarea里Enter(回车换行符)
    uniApp打卡日历
  • 原文地址:https://www.cnblogs.com/phyxis/p/5474023.html
Copyright © 2011-2022 走看看