import sun.security.pkcs.PKCS7;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.security.GeneralSecurityException;import java.security.cert.X509Certificate;class SignApk { public static X509Certificate readSignatureBlock(InputStream in) throws IOException, GeneralSecurityException { PKCS7 pkcs7 = new PKCS7(in); return pkcs7.getCertificates()[0]; } public static void main(String[] args) throws FileNotFoundException, IOException, GeneralSecurityException { X509Certificate publicKey = readSignatureBlock(new FileInputStream( "D:\cert.rsa")); System.out.println("issuer:" + publicKey.getIssuerDN()); System.out.println("subject:" + publicKey.getSubjectDN()); System.out.println(publicKey.getPublicKey()); }}
The output content is as follows:
Issuer: Cn = Zhang ZM
Subject: Cn = Zhang ZM
Sun RSA public keys, 1024 bits
Modulus: 1543085941444687053482947604843962642184531 ......
Certificate ------------------------------------------------------------------------------------------------------------------------------------------------
Android signature and Public Key acquisition methods
Private byte [] getsign (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; // obtain the signature by package name if (packagename. equals ("com. test. 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)); 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;}