Unity3d how to get the APK signature and public key

Source: Internet
Author: User
Tags modulus types of functions

The core idea of getting the APK package signature public key in the Unity3d project is to invoke the methods provided by the Android via JNI. However, Unity3d provides a layer of class androidjavaobject than Jni and inherits its Androidjavaclass, helping developers save a lot of work. These two types of functions and internal principles can be seen Unity3d official website, you can also decompile to see the code inside, directly said usage.

The first thing to do is to get the activity of the currently running program before proceeding. Unity3d This value is recorded at program startup and can be obtained by currentactivity the static field of the Unityplayer class. Unityplayer is created and passed into the current activity by unityplayeractivity, a class that inherits from activity at OnCreate (the detailed process can be viewed by Jd-gui or by an anti-compilation project Smali). After the activity is taken, you can getpackagemanager, getpackageinfo to obtain the signature. The resulting signature is said to hold the public key and some other information, this does not have a specific insight, but can be obtained from the signature to the public key is certainly possible, certificatefactory Generatecertificate, Getpublickey method obtains the final value. Here is the complete code, which corresponds to the Java code:

 Private Byte[] Getsignature() { Player = new Unityplayer (); Androidjavaclass Player = New Androidjavaclass("Com.unity3d.player.UnityPlayer"); Activity = player.currentactivity; Androidjavaobject Activity = Player.Getstatic<Androidjavaobject> ("CurrentActivity"); Packagemanager = Activity.getpackagemanager (); Androidjavaobject Packagemanager = Activity.Pager<Androidjavaobject> ("Getpackagemanager"); Get_signatures = Packagemanager.get_signatures; IntGet_signatures= Packagemanager.Getstatic<int>("Get_signatures"); PackageInfo = Packagemanager.getpackageinfo ("Com.ztx.uni", packagemanager.get_signatures); Androidjavaobject PackageInfo = Packagemanager.Pager<Androidjavaobject> ("Getpackageinfo", "Com.ztx.uni",Get_signatures); Signatures = Packageinfo.signatures; Androidjavaobject[] Signatures = PackageInfo.Get<Androidjavaobject[]> ("Signatures"); return Signatures[0].tobytearray (); Return (Signatures != Null && Signatures.Length > 0) ? Signatures[0].Pager<Byte[]> ("Tobytearray") : Null; } Private String Getpublickey() { Byte[] Signatures = Getsignature(); If (Signatures != Null) { Ccertificatefactory = new Certificatefactory (); Androidjavaclass Ccertificatefactory = New Androidjavaclass("Java.security.cert.CertificateFactory"); Ocertfactory = Ccertificatefactory.getinstance (); Androidjavaobject Ocertfactory = Ccertificatefactory.Callstatic<Androidjavaobject> ("GetInstance", "The"); Obytearrayinputstream = new Bytearrayinputstream (Signatures); Androidjavaobject Obytearrayinputstream = New Androidjavaobject("Java.io.ByteArrayInputStream", Signatures); Ox509certificate = Ocertfactory.generatecertificate (Obytearrayinputstream); AndroidjavaobjectOx509certificate= Ocertfactory.Pager<Androidjavaobject> ("Generatecertificate", Obytearrayinputstream); Opublickey = Ox509certificate.getpublickey (); Androidjavaobject Opublickey =Ox509certificate.Pager<Androidjavaobject> ("Getpublickey"); StringPublicKey= Opublickey.Pager<string>("ToString"); Androidjavaobject CString = New Androidjavaobject("Java/lang/string",PublicKey); IntStart= CString.Pager<int>("IndexOf", "Modulus"); Int End = CString.Pager<int>("IndexOf", "Public"); If (Start>= 0 && End >= 0  { returncstring. Call<string> ( "substring"  start + 8 end -  1 } } return  null;               /span>                




The Opublickey obtained in Getpublickey () is in the following form:
opensslrsapublickey{modulus=  D6931904dec60b24b1edc762e0d9d8253e3ecd6ceb1de2ff068ca8e8bca8cd6bd3786ea70aa76ce  60ebb0f993559ffd93e77a943e7e83d4b64b8e4fea2d3e656f1e267a81bbfb230b578c20443be4c7218b846f  5211586f038a14e89c2be387f8ebecf8fcac3da1ee330c9ea93d0a7c3dc4af350220d50080732e0809717ee6  A053359e6a694ec2cb3f284a0a466c87a94d83b31093a67372e2f6412c06e6d42f15818dffe0381cc0cd444da6cddc3b82458194801b32564134fbfde 98c9287748dbf5676a540d8154c8bbca07b9e247553311c46b9af76fdeeccc8e69e7c8a2d08e782620943f99727d3c04fe72991d99df9bae38a0b2177 fa31d5b6afee91f,publicexponent=3}         


All we need is a 256-bit public key, so we take the value between modulus and publicexponent after the function. At this point all the work is done, it is very simple, if you do public key extraction in the NDK is the same, but to write a lot of code.


Reprint Address: http://m.blog.csdn.net/blog/s10141303/38260469

Unity3d how to get the APK signature and public key

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.