Android 获取签名、公钥方法

1. [代码][Java]代码     

01 private byte[] getSign(Context context) {
02         PackageManager pm = context.getPackageManager();
03         List<PackageInfo> apps = pm
04                 .getInstalledPackages(PackageManager.GET_SIGNATURES);
05         Iterator<PackageInfo> iter = apps.iterator();
06  
07         while (iter.hasNext()) {
08             PackageInfo info = iter.next();
09             String packageName = info.packageName;
10             //按包名 取签名
11             if (packageName.equals("com.test.test")) {
12                 return info.signatures[0].toByteArray();
13                  
14             }
15         }
16         return null;
17     }

2. [代码][Java]代码     

01 public static String getPublicKey(byte[] signature) {
02         try {
03  
04             CertificateFactory certFactory = CertificateFactory
05                     .getInstance("X.509");
06             X509Certificate cert = (X509Certificate) certFactory
07                     .generateCertificate(new ByteArrayInputStream(signature));
08  
09             String publickey = cert.getPublicKey().toString();
10             publickey = publickey.substring(publickey.indexOf("modulus: ") + 9,
11                     publickey.indexOf("\n", publickey.indexOf("modulus:")));
12  
13             Log.d("TRACK", publickey);
14             return publickey;
15         catch (CertificateException e) {
16             e.printStackTrace();
17         }
18         return null;
19     }
20     

你可能感兴趣的:(Android 获取签名、公钥方法)