Android 读取APK签名信息
2014-10-21 22:42
639 查看
[java]
view plaincopy
某些时候需要获取某个特定的apk(已安装或者未安装)的签名信息,如程序自检测,可信赖的第三方检测(应用市场),系统限定安装
对此,有两种实现方法
可以使用Java自带的API(主要用到的为JarFile,JarEntry,Certificate)进行获取,还有一种方法是使用系统隐藏的API PackageParser,通过反射来使用对应的API.
但是由于安卓系统的分裂版本过多,并且不同厂商进行的修改很多,依赖反射隐藏API的方法并不能保证兼容性和通用性,因此推荐使用JAVA自带API进行获取:
/**
* 从APK中读取签名
* @param file
* @return
* @throws IOException
*/
private static List<String> getSignaturesFromApk(File file) throws IOException {
List<String> signatures=new ArrayList<String>();
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
/**
* 加载签名
* @param jarFile
* @param je
* @param readBuffer
* @return
*/
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;
}
/**
* 将签名转成转成可见字符串
* @param sigBytes
* @return
*/
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);
}
view plaincopy
某些时候需要获取某个特定的apk(已安装或者未安装)的签名信息,如程序自检测,可信赖的第三方检测(应用市场),系统限定安装
对此,有两种实现方法
可以使用Java自带的API(主要用到的为JarFile,JarEntry,Certificate)进行获取,还有一种方法是使用系统隐藏的API PackageParser,通过反射来使用对应的API.
但是由于安卓系统的分裂版本过多,并且不同厂商进行的修改很多,依赖反射隐藏API的方法并不能保证兼容性和通用性,因此推荐使用JAVA自带API进行获取:
/**
* 从APK中读取签名
* @param file
* @return
* @throws IOException
*/
private static List<String> getSignaturesFromApk(File file) throws IOException {
List<String> signatures=new ArrayList<String>();
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
/**
* 加载签名
* @param jarFile
* @param je
* @param readBuffer
* @return
*/
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;
}
/**
* 将签名转成转成可见字符串
* @param sigBytes
* @return
*/
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的签名信息,可用作防盗版进入。
- [Android Pro] 查看 keystore文件的签名信息 和 检查apk文件中的签名信息
- 获取Android的APK包签名信息
- 纯JAVA读取android应用程序apk包的相关信息
- Android检测版本更新(读取apk配置文件中的版本信息)
- android读取apk中已经存在的数据库信息
- [置顶] Android学习——查看apk签名 和 keystore 的信息
- Android学习——查看apk签名 和 keystore 的信息
- Android:获取APK签名信息
- 使用Android系统隐藏api 读取*.apk 程序安装包信息[转]
- 读取APK包中配置文件 AndroidManifest.xml 中的版本信息
- Android:获取APK签名信息 .
- 使用Android系统隐藏api 读取*.apk 程序安装包信息