图片所示:
方法:【亲测有效】
protected String getSignInfo(Context context,String packageName) {
String signcode ="";//公钥
try {
List packages = context.getPackageManager().getInstalledPackages(PackageManager.GET_SIGNATURES);
for (PackageInfo packageInfo : packages){
if (packageInfo.packageName.equals(packageName)) {
Signature[] signs = packageInfo.signatures;
Signature sign = signs[0];
signcode =parseSignature(sign.toByteArray());
String md5=getSignValidString(sign.toByteArray());
tv_md5.setText(md5);
signcode = signcode.toLowerCase();
}
}
}catch (Exception E) {
// Log.e(Constants.TAG,e.getmessage(),E);
}
return signcode;
}
protected static StringparseSignature(byte[] signaturE) {
String sign ="";
try {
CertificateFactory certFactory = CertificateFactory
.getInstance("X.509");
X509Certificate cert = (X509Certificate) certFactory
.generateCertificate(new ByteArrayInputStream(signaturE));
String pubKey = cert.getPublicKey().toString();
String ss =subString(pubKey);
ss = ss.replace(",","");
ss = ss.toLowerCase();
int aa = ss.indexOf("modulus");
int bb = ss.indexOf("publicexponent");
sign = ss.substring(aa+8,bb);
}catch ( java.security.cert.CertificateException E) {
Log.e("CertificateException",E.toString());
}
return sign;
}
public static StringsubString(String sub) {
Pattern pp = Pattern.compile("\\s*|\t|\r|\n");
Matcher mm = pp.matcher(sub);
return mm.replaceAll("");
}
private StringgetSignValidString(byte[] paramArrayOfByte)throws NoSuchAlgorithmException {
MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
localMessageDigest.update(paramArrayOfByte);
return toHexString(localMessageDigest.digest());
}
public StringtoHexString(byte[] paramArrayOfByte) {
if (paramArrayOfByte ==null) {
return null;
}
StringBuilder localStringBuilder =new StringBuilder(2 * paramArrayOfByte.length);
for (int i =0; ; i++) {
if (i >= paramArrayOfByte.length) {
return localStringBuilder.toString();
}
String str = Integer.toString(0xFF & paramArrayOfByte[i], 16);
if (str.length() ==1) {
str ="0" + str;
}
localStringBuilder.append(str);
}
}