public class getApkSign {
public static void main(String[] args) throws IOException, Exception {
String path = "E:\\android\\workspace\\trunk\\ccplay_2016\\CCPlay_Market\\build\\outputs\\apk\\CCPlay_Market-debug.apk";// apk的路径
byte[] bytes = getSignaturesFromApk(path);
System.out.println(hexDigest(bytes));
}
public static String hexDigest(byte[] bytes) {
MessageDigest md5;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
e.printStackTrace();
return "";
}
byte[] md5Bytes = md5.digest(bytes);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
/**
* 从APK中读取签名
*
* @param file
* @return
* @throws IOException
*/
private static byte[] getSignaturesFromApk(String strFile) throws IOException {
File file = new File(strFile);
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) {
return c.getEncoded();
}
}
} catch (Exception ex) {
}
return null;
}
/**
* 加载签名
*
* @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;
}
}
java 层获取 apk签名
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 在发布应用的时候,需要通过keystore文件对apk包进行签名,才能发布到市场上。在使用微信平台服务时或者其他需...
- Android签名问题 Android平台的游戏包大多数都遇到过被破解和二次打包的问题,android平台本身的开...