Python获取Android Apk签名信息

#!/usr/bin/python
#coding:utf-8
import sys,os,commands,re
reload(sys)
sys.setdefaultencoding('utf8')

def get_signature(file = "test.apk") :
    #resule = commands.getstatusoutput("pwd")
    #查找cert在apk路径
    comm_path = "jar tf " + file + " | grep RSA"
    path = commands.getstatusoutput(comm_path)[1]
    #解压证书文件
    comm_unzip = "jar xf " +? " " + file + " " +? path
    unzip = commands.getstatusoutput(comm_unzip)
    #提取证书信息
    comm_cert = "keytool -printcert -file " + path
    cert = commands.getstatusoutput(comm_cert)[1]
    #删除解压的文件
    comm_rm = "rm -rf " + path
    rm_del = commands.getstatusoutput(comm_rm)
    #返回证书信息
    return cert

#demo1 = get_signature("162b81bea7cf22b5cfb182f991f6b124.apk")
#print demo1
#demo2 = get_signature("com.laijin.simplefinance.apk")
#print demo2
#demo3 = get_signature("com.laijin.simplefinance_043204.apk")
#print demo3

#demo1 = get_signature("8.apk")
#print demo1
#demo2 = get_signature("7.apk")
#print demo2
#demo3 = get_signature("6.apk")
#print demo3

demo3 = get_signature("6.apk")
print demo3

"""
test = demo3
rex = u"[\xe5\xba\x8f\xe5\x88\x97\xe5\x8f\xb7:]"+"(.*?)"+u"[\\xe6\x9c\x89\xe6\x95\x88\xe6\x9c\x9f:]"
regx = re.compile(rex)
s2 = regx.findall(test) #([\u5e8f\u5217\u53f7\u003a])+(.*?)+([\u5e8f\u5217\u53f7])
print s2
"""

#获取签名键值
def get_value(line):
    index = line.find(':') #第一次出现的位置
    resule = line[index+1:] #第一次出现的位置截止后的字符串
    return resule

#过滤获取签名返回值
def resule_signature(signature):
    #定义返回结果
    resule ={}
    #定义分割的每行数据
    lines = []
    #分割每一行生成list
    for line in signature.split("\n"):
    lines.append(line)
    #print lines #打印签名序列
    resule['suoyouzhe'] = get_value(lines[0])
    resule['qianfaren'] = get_value(lines[1])
    resule['xuliehao'] = get_value(lines[2])
    resule['youxiaoqi'] = get_value(lines[3])
    resule['zhengshu'] = {}
    resule['zhengshu']['md5'] = get_value(lines[5])
    resule['zhengshu']['sha1'] = get_value(lines[6])
    resule['zhengshu']['suanfa'] = get_value(lines[7])
    resule['zhengshu']['version'] = get_value(lines[8])
    return resule

    resule = resule_signature(demo3)

"""
#提取序列号
xuliehao = lines[2]
"""
#获取签名键值
def get_value(arr):
    value = []
    index = arr.find(':')
    print index

    for line in arr.split(":"):
    value.append(line)
    value = value[1]
    print u">>>>>>>>>>>>",value
    return value

发表评论

电子邮件地址不会被公开。 必填项已用*标注