可以利用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; } }
时间: 2024-10-12 12:01:05