Android - 在app运行时访问自己的公钥?哪个密钥对用于签署apk

时间:2013-08-15 10:26:31

标签: android

如何通过Android API获取base64公钥?

2 个答案:

答案 0 :(得分:4)

要获取公共证书,您需要在代码中使用PackageManager

PackageInfo packageInfo = null;

try {
   packageInfo = getPackageManager().getPackageInfo(getPackageName(), 
                                           PackageManager.GET_SIGNATURES);

   Signature[] signatures = packageInfo.signatures;
   byte[] cert = signatures[0].toByteArray();
   InputStream input = new ByteArrayInputStream(cert);

   CertificateFactory cf = CertificateFactory.getInstance("X509");
   X509Certificate c = (X509Certificate)cf.generateCertificate(input);

   PublicKey key = c.getPublicKey();

   ...   

} catch ( Exception e) {
        e.printStackTrace();
}

答案 1 :(得分:1)

公钥存储在apk内的META-INF / CERT.RSA中。