获取签名公钥的代码(未测)

来源:互联网 发布:mac怎么同时开两个窗口 编辑:程序博客网 时间:2024/06/16 06:12
  1. public class GetPublicKey {  
  2.       
  3.     /** 
  4.      * 获取签名公钥 
  5.      * @param mContext 
  6.      * @return 
  7.      */  
  8.     protected static String getSignInfo(Context mContext) {  
  9.         String signcode = "";  
  10.         try {  
  11.             PackageInfo packageInfo = mContext.getPackageManager().getPackageInfo(  
  12.                     GetAppInfo.getPackageName(mContext), PackageManager.GET_SIGNATURES);  
  13.             Signature[] signs = packageInfo.signatures;  
  14.             Signature sign = signs[0];  
  15.   
  16.             signcode = parseSignature(sign.toByteArray());  
  17.             signcode = signcode.toLowerCase();  
  18.         } catch (Exception e) {  
  19.             Log.e(Constants.TAG, e.getMessage(), e);  
  20.         }  
  21.         return signcode;  
  22.     }  
  23.   
  24.     protected static String parseSignature(byte[] signature) {  
  25.         String sign = "";  
  26.         try {  
  27.             CertificateFactory certFactory = CertificateFactory  
  28.                     .getInstance("X.509");  
  29.             X509Certificate cert = (X509Certificate) certFactory  
  30.                     .generateCertificate(new ByteArrayInputStream(signature));  
  31.             String pubKey = cert.getPublicKey().toString();  
  32.             String ss = subString(pubKey);  
  33.             ss = ss.replace(",""");  
  34.             ss = ss.toLowerCase();  
  35.             int aa = ss.indexOf("modulus");  
  36.             int bb = ss.indexOf("publicexponent");  
  37.             sign = ss.substring(aa + 8, bb);  
  38.         } catch (CertificateException e) {  
  39.             Log.e(Constants.TAG, e.getMessage(), e);  
  40.         }  
  41.         return sign;  
  42.     }  
  43.   
  44.     public static String subString(String sub) {  
  45.         Pattern pp = Pattern.compile("\\s*|\t|\r|\n");  
  46.         Matcher mm = pp.matcher(sub);  
  47.         return mm.replaceAll("");  
  48.     }  
  49. }  
0 0
原创粉丝点击