This article describes the Android implementation of the method of acquiring signatures and public keys. Share to everyone for your reference. Specifically as follows:
1. The Java code is as follows:
Private byte[] Getsign (context context) {
Packagemanager pm = Context.getpackagemanager ();
List<packageinfo> apps = pm
. getinstalledpackages (packagemanager.get_signatures);
Iterator<packageinfo> iter = Apps.iterator ();
while (Iter.hasnext ()) {
PackageInfo info = Iter.next ();
String packagename = info.packagename;
Sign
if (packagename.equals ("Com.test.test")) by package name {return
info.signatures[0].tobytearray ();
}
} return
null;
}
2. The Java code is as follows:
public static String Getpublickey (byte[] signature) {
try {
certificatefactory certfactory = certificatefactory
. getinstance ("X.509");
X509Certificate cert = (x509certificate) certfactory
. Generatecertificate (New Bytearrayinputstream (signature));
String PublicKey = Cert.getpublickey (). toString ();
PublicKey = publickey.substring (Publickey.indexof ("modulus:") + 9,
publickey.indexof ("\ n", Publickey.indexof (" Modulus: "));
LOG.D ("TRACK", PublicKey);
return publickey;
} catch (Certificateexception e) {
e.printstacktrace ();
}
return null;
}
I hope this article will help you with your Android program.