10

I would like to programmatically perform a check on the APK's signature at runtime. I own a keystore on my development workstation, so I could know (dunno how) the public key I'm signing an APK with.

Once I know what the public key will be after signage, I would like to put in the source code and check if the currently running application matches the key.

Is it possible? If so, how do I obtain the public key from an Eclipse-generated keystore?

Thanks.

usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305

2 Answers2

6

You could try this, it should work

Signature[] sigs = getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES).signatures;
    for (Signature sig : sigs)
    {
        // log the sig here
    }
Faisal Abid
  • 8,900
  • 14
  • 59
  • 91
  • 1
    Does not work after uploading on playstore. Tried the apk before uploading on playstore, it works. But after uploading, the signature changes. – Tenten Ponce Feb 19 '20 at 06:28
  • @TentenPonce what is solution? your app signature changed after upload on google play? – Farzad Mar 04 '20 at 15:27
  • @Farzad still looking for another solution, but we could try to publish a pre-version to check the signature on playstore then re-release again with the signature that we got from the playstore app. – Tenten Ponce Mar 04 '20 at 16:24
  • @TentenPonce Can saboteurs neutralize this solution by changing the Java code? – Farzad Mar 04 '20 at 18:57
  • 1
    @Farzad well yes, they can still remove the code that checks for signature. There's no silver bullet in security as they say. You can only make it hard but not totally prevent. – Tenten Ponce Mar 09 '20 at 01:32
  • why does signature change after uploading to the play store? – D.madushanka Feb 03 '21 at 04:30
  • @D.madushanka because Play Store re-signs your key using their own signing. The signing key you have used the first time is then used to verify you're the one uploading the apk/aab. You can turn this off, but it is not recommended. – Abdullah Z Khan Jan 31 '22 at 04:59
0

I think I have the same situation like you have. Here is my original solution.

You may have a try on it.

Signature sig = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES).signatures[0];
Signature releaseSig = context.getPackageManager().getPackageAchiveInfo("/mnt/sdcard/myReleaseApk.apk", PackageManager.GET_SIGNATURES).signatures[0];
return sig.hashCode() == releaseSig.hashCode;
Community
  • 1
  • 1
Shengfeng Li
  • 606
  • 7
  • 11