android 读取apk签名信息
2013-08-07 14:33
274 查看
某些时候需要获取某个特定的apk(已安装或者未安装)的签名信息,如程序自检测,可信赖的第三方检测(应用市场),系统限定安装
对此,有两种实现方法
可以使用Java自带的API(主要用到的为JarFile,JarEntry,Certificate)进行获取,还有一种方法是使用系统隐藏的API PackageParser,通过反射来使用对应的API.
但是由于安卓系统的分裂版本过多,并且不同厂商进行的修改很多,依赖反射隐藏API的方法并不能保证兼容性和通用性,因此推荐使用JAVA自带API进行获取:
private static List getSignaturesFromApk(File file) throws IOException {
List signatures=new ArrayList();
JarFile jarFile=new JarFile(file);
try {
JarEntry je=jarFile.getJarEntry("AndroidManifest.xml");
byte[] readBuffer=new byte[8192];
Certificate[] certs=loadCertificates(jarFile, je, readBuffer);
if(certs != null) {
for(Certificate c: certs) {
String sig=toCharsString(c.getEncoded());
signatures.add(sig);
}
}
} catch(Exception ex) {
}
return signatures;
}
[java] view plaincopy
private static Certificate[] loadCertificates(JarFile jarFile, JarEntry je, byte[] readBuffer) {
try {
InputStream is=jarFile.getInputStream(je);
while(is.read(readBuffer, 0, readBuffer.length) != -1) {
}
is.close();
return je != null ? je.getCertificates() : null;
} catch(IOException e) {
}
return null;
}
private static String toCharsString(byte[] sigBytes) {
byte[] sig=sigBytes;
final int N=sig.length;
final int N2=N * 2;
char[] text=new char[N2];
for(int j=0; j < N; j++) {
byte v=sig[j];
int d=(v >> 4) & 0xf;
text[j * 2]=(char)(d >= 10 ? ('a' + d - 10) : ('0' + d));
d=v & 0xf;
text[j * 2 + 1]=(char)(d >= 10 ? ('a' + d - 10) : ('0' + d));
}
return new String(text);
}
对此,有两种实现方法
可以使用Java自带的API(主要用到的为JarFile,JarEntry,Certificate)进行获取,还有一种方法是使用系统隐藏的API PackageParser,通过反射来使用对应的API.
但是由于安卓系统的分裂版本过多,并且不同厂商进行的修改很多,依赖反射隐藏API的方法并不能保证兼容性和通用性,因此推荐使用JAVA自带API进行获取:
private static List getSignaturesFromApk(File file) throws IOException {
List signatures=new ArrayList();
JarFile jarFile=new JarFile(file);
try {
JarEntry je=jarFile.getJarEntry("AndroidManifest.xml");
byte[] readBuffer=new byte[8192];
Certificate[] certs=loadCertificates(jarFile, je, readBuffer);
if(certs != null) {
for(Certificate c: certs) {
String sig=toCharsString(c.getEncoded());
signatures.add(sig);
}
}
} catch(Exception ex) {
}
return signatures;
}
[java] view plaincopy
private static Certificate[] loadCertificates(JarFile jarFile, JarEntry je, byte[] readBuffer) {
try {
InputStream is=jarFile.getInputStream(je);
while(is.read(readBuffer, 0, readBuffer.length) != -1) {
}
is.close();
return je != null ? je.getCertificates() : null;
} catch(IOException e) {
}
return null;
}
private static String toCharsString(byte[] sigBytes) {
byte[] sig=sigBytes;
final int N=sig.length;
final int N2=N * 2;
char[] text=new char[N2];
for(int j=0; j < N; j++) {
byte v=sig[j];
int d=(v >> 4) & 0xf;
text[j * 2]=(char)(d >= 10 ? ('a' + d - 10) : ('0' + d));
d=v & 0xf;
text[j * 2 + 1]=(char)(d >= 10 ? ('a' + d - 10) : ('0' + d));
}
return new String(text);
}
相关文章推荐
- Android 读取APK签名信息
- Android 如何读取未安装的APK 签名信息
- Android 读取APK签名信息
- Android 如何读取APK 签名信息
- Android 读取APK签名信息
- Android 读取APK签名信息
- Android 读取APK签名信息
- Android获取apk签名信息(举例说明java反射调用方法步骤)
- android 读取U盘apk文件信息,拔掉U盘导致进程被杀
- [Android Pro] 查看 keystore文件的签名信息 和 检查apk文件中的签名信息
- 如何读取APK中的签名信息
- 读取APK包中配置文件 AndroidManifest.xml 中的版本信息
- Android学习——查看apk签名 和 keystore 的信息
- android获取APK签名信息及MD5指纹
- Android Studio apk打包以及获取apk签名信息
- android读取apk中已经存在的数据库信息
- Android获取apk程序签名信息代码示例
- Android 获取 APK 签名信息及 MD5 指纹
- android获取APK签名信息及MD5指纹
- android获取未安装APK签名信息及MD5指纹