Android 獲取應(yīng)用 MD5 SHA1 SHA256 簽名信息

2020-2-8    seo達(dá)人

閑著沒(méi)事兒寫(xiě)了個(gè)小 demo ,獲取手機(jī)上已安裝應(yīng)用信息,系統(tǒng)應(yīng)用和 非系統(tǒng)應(yīng)用

MD5 SHA1 SHA256 簽名信息 點(diǎn)擊簽名信息可復(fù)制到剪切板,

GitHub:https://github.com/sunan-n/GetAppInfo

如下圖:







<span style="white-space:pre;"> </span>主要就是這個(gè)方法,傳參數(shù)進(jìn)來(lái)獲取相應(yīng)的簽名類(lèi)型 信息<br />
&nbsp; &nbsp; public static String getSignaturesInfo(Context context, String packageName, String tpye) {<br />
//&nbsp; &nbsp; &nbsp; &nbsp; //獲取包管理器<br />
&nbsp; &nbsp; &nbsp; &nbsp; PackageManager pm = context.getPackageManager();<br />
&nbsp; &nbsp; &nbsp; &nbsp; //返回包括在包中的簽名信息<br />
&nbsp; &nbsp; &nbsp; &nbsp; int flags = PackageManager.GET_SIGNATURES;<br />
&nbsp; &nbsp; &nbsp; &nbsp; PackageInfo packageInfo = null;<br />
&nbsp; &nbsp; &nbsp; &nbsp; try {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //獲得包的所有內(nèi)容信息類(lèi)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; packageInfo = pm.getPackageInfo(packageName, flags);<br />
&nbsp; &nbsp; &nbsp; &nbsp; } catch (PackageManager.NameNotFoundException e) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; //簽名信息<br />
&nbsp; &nbsp; &nbsp; &nbsp; Signature[] signatures = packageInfo.signatures;<br />
&nbsp; &nbsp; &nbsp; &nbsp; byte[] cert = signatures[0].toByteArray();<br />
&nbsp; &nbsp; &nbsp; &nbsp; //將簽名轉(zhuǎn)換為字節(jié)數(shù)組流<br />
&nbsp; &nbsp; &nbsp; &nbsp; InputStream input = new ByteArrayInputStream(cert);<br />
&nbsp; &nbsp; &nbsp; &nbsp; //證書(shū)工廠(chǎng)類(lèi),這個(gè)類(lèi)實(shí)現(xiàn)了出廠(chǎng)合格證算法的功能<br />
&nbsp; &nbsp; &nbsp; &nbsp; CertificateFactory cf = null;<br />
&nbsp; &nbsp; &nbsp; &nbsp; try {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cf = CertificateFactory.getInstance("X509");<br />
&nbsp; &nbsp; &nbsp; &nbsp; } catch (CertificateException e) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; //X509證書(shū),X.509是一種非常通用的證書(shū)格式<br />
&nbsp; &nbsp; &nbsp; &nbsp; X509Certificate c = null;<br />
&nbsp; &nbsp; &nbsp; &nbsp; try {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c = (X509Certificate) cf.generateCertificate(input);<br />
&nbsp; &nbsp; &nbsp; &nbsp; } catch (CertificateException e) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; String hexString = null;<br />
&nbsp; &nbsp; &nbsp; &nbsp; try {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //加密算法的類(lèi),這里的參數(shù)可以使MD4,MD5等加密算法<br />
//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageDigest md = MessageDigest.getInstance("SHA1");<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageDigest md = MessageDigest.getInstance(tpye);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //獲得公鑰<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] publicKey = md.digest(c.getEncoded());<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //字節(jié)到十六進(jìn)制的格式轉(zhuǎn)換<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hexString = byte2HexFormatted(publicKey);<br />
&nbsp; &nbsp; &nbsp; &nbsp; } catch (NoSuchAlgorithmException e1) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e1.printStackTrace();<br />
&nbsp; &nbsp; &nbsp; &nbsp; } catch (CertificateEncodingException e) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; return hexString;<br />
&nbsp; &nbsp; }<br />
<br />

分享本文至:

日歷

鏈接

個(gè)人資料

存檔