Skip to content

Instantly share code, notes, and snippets.

@venator85
Created October 24, 2016 10:41
Show Gist options
  • Save venator85/7659ee61252de4fbdc74295250b8ecd8 to your computer and use it in GitHub Desktop.
Save venator85/7659ee61252de4fbdc74295250b8ecd8 to your computer and use it in GitHub Desktop.
Extract the X509Certificate used to sign the app from the APK
package com.example.alessio.testapp;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import java.io.IOException;
import java.io.InputStream;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ApkCertificate {
public static X509Certificate getApkCertificate(Context context) throws Exception {
ZipFile apk = null;
InputStream in = null;
try {
ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);
apk = new ZipFile(ai.sourceDir);
ZipEntry entry = apk.getEntry("META-INF/CERT.RSA");
in = apk.getInputStream(entry);
CertificateFactory factory = CertificateFactory.getInstance("X.509");
return (X509Certificate) factory.generateCertificate(in);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignore) {
}
}
if (apk != null) {
try {
apk.close();
} catch (IOException ignore) {
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment