Created
December 1, 2016 05:30
-
-
Save EdenStack/57d706006b3bcfaa2eafcbc025d50426 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.test.mytest; | |
import java.io.ByteArrayInputStream; | |
import java.security.cert.CertificateException; | |
import java.security.cert.CertificateFactory; | |
import java.security.cert.X509Certificate; | |
import java.util.Iterator; | |
import java.util.List; | |
import android.content.Context; | |
import android.content.pm.PackageInfo; | |
import android.content.pm.PackageManager; | |
import android.util.Log; | |
public class MyTestClass1 { | |
public void CheckSignature(Context context){ | |
byte[] signature = getSign(context); | |
String publickey = getPublicKey(signature); | |
Log.d("CheckSignature", "publickey = " + publickey); | |
} | |
private byte[] getSign(Context context) { | |
PackageManager pm = context.getPackageManager(); | |
List<PackageInfo> apps = pm.getInstalledPackages(PackageManager.GET_SIGNATURES); | |
Iterator<PackageInfo> iter = apps.iterator(); | |
while (iter.hasNext()) { | |
PackageInfo info = iter.next(); | |
String packageName = info.packageName; | |
//按包名读取签名 | |
if (packageName.equals("com.example.resign_test")) { | |
return info.signatures[0].toByteArray(); | |
} | |
} | |
return null; | |
} | |
public static String getPublicKey(byte[] signature) { | |
try { | |
CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); | |
X509Certificate cert = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(signature)); | |
return cert.getPublicKey().toString(); | |
} catch (CertificateException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment