wez
wez

Reputation: 337

Android - access the public key of its own in app runtime? Which the key-pair was used to sign the apk

How to get that base64 public key through Android API?

Upvotes: 2

Views: 2151

Answers (2)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

To get your public certificate you need to ise PackageManager in your code:

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();
}

Upvotes: 5

mach
mach

Reputation: 8395

The public key is stored in the META-INF/CERT.RSA inside the apk.

Upvotes: 1

Related Questions