) Read signature information

Source: Internet
Author: User
Tags modulus pkcs7
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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.