Android实现获取签名及公钥的方法
程序员文章站
2024-03-31 09:06:04
本文实例讲述了android实现获取签名及公钥的方法。分享给大家供大家参考。具体如下:
1. java代码如下:
private byte[] ge...
本文实例讲述了android实现获取签名及公钥的方法。分享给大家供大家参考。具体如下:
1. java代码如下:
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.test.test")) { return info.signatures[0].tobytearray(); } } return null; }
2. java代码如下:
public static string getpublickey(byte[] signature) { try { certificatefactory certfactory = certificatefactory .getinstance("x.509"); x509certificate cert = (x509certificate) certfactory .generatecertificate(new bytearrayinputstream(signature)); string publickey = cert.getpublickey().tostring(); publickey = publickey.substring(publickey.indexof("modulus: ") + 9, publickey.indexof("\n", publickey.indexof("modulus:"))); log.d("track", publickey); return publickey; } catch (certificateexception e) { e.printstacktrace(); } return null; }
希望本文所述对大家的android程序设计有所帮助。