The corresponding Java method:
voidGetsign (Context context)throwsException {PackageInfo localpackageinfo = Context.getpackagemanager (). Getpackageinfo (Context.getpackagenam E (), -); StringBuilder Localstringbuilder =NewStringBuilder (); signature[] Arrayofsignature = localpackageinfo.signatures; Signature localsignature = arrayofsignature[0]; MessageDigest localmessagedigest = messagedigest.getinstance ("MD5"); Localmessagedigest.update (Localsignature.tobytearray ());//This is the MD5 value of the signatureString str2 = Tohex (Localmessagedigest.digest ());}PrivateStringTohex(byte[] paramarrayofbyte) {StringBuffer Localstringbuffer =NewStringBuffer (); for(inti =0; i < paramarrayofbyte.length; i++) {object[] Arrayofobject =Newobject[1]; arrayofobject[0] = byte.valueof (Paramarrayofbyte[i]); Localstringbuffer.append (String.Format ("%02x", Arrayofobject)); }returnLocalstringbuffer.tostring ();}
In JNI, you can call Java methods, and refer to the Java method above to write the C code of JNI:
//byte stream converted to hexadecimal string//http://blog.csdn.net/pingd/article/details/41945417voidBYTETOHEXSTR (Const unsigned Char* Source,Char* Dest,intSourcelen) { ShortIunsigned CharHighbyte, Lowbyte; for(i =0; i < Sourcelen; i++) {highbyte = Source[i] >>4; Lowbyte = Source[i] &0x0f; Highbyte + =0x30;if(Highbyte >0x39) Dest[i *2] = Highbyte +0x07;ElseDest[i *2] = Highbyte; Lowbyte + =0x30;if(Lowbyte >0x39) Dest[i *2+1] = Lowbyte +0x07;ElseDest[i *2+1] = Lowbyte; }return;} Jniexport jstring jnicall java_com_hengbao_util_decodeutil_test (jnienv * env, jobject thiz, Jobject context) {//java code======================================================= //PackageInfo Localpackageinfo = Context.getpackagemanager () //. Getpackageinfo (Context.getpackagename (), +); //StringBuilder Localstringbuilder = new StringBuilder (); //signature[] arrayofsignature = localpackageinfo.signatures; //Signature localsignature = arrayofsignature[0]; //MessageDigest localmessagedigest = messagedigest.getinstance ("MD5"); //Localmessagedigest.update (Localsignature.tobytearray ()); ////This is the MD5 value of the signature //String str2 = Tohex (Localmessagedigest.digest ()); //Localstringbuilder.append ("Signature of") //. Append (Localpackageinfo.packagename). append (' \ n '). Append (str2); //System.out.println (localstringbuilder.tostring ()); //TextView TV = (TextView) Findviewbyid (r.id.tv); //Tv.settext (localstringbuilder.tostring ()); //Private String Tohex (byte[] paramarrayofbyte) { //StringBuffer Localstringbuffer = new StringBuffer (); //for (int i = 0; i < paramarrayofbyte.length; i++) { //object[] Arrayofobject = new object[1]; //Arrayofobject[0] = byte.valueof (Paramarrayofbyte[i]); //Localstringbuffer.append (String.Format ("%02x", Arrayofobject)); // } //return localstringbuffer.tostring (); // } //java code======================================================Jclass Tem_class; Jmethodid Tem_method; Jclass Class_context = (*env)->getobjectclass (env, context);//PackageInfo Localpackageinfo = Context.getpackagemanager () //. Getpackageinfo (Context.getpackagename (), +);Tem_method = (*env)->getmethodid (env, Class_context,"Getpackagemanager","() Landroid/content/pm/packagemanager;"); Jobject Obj_package_manager = (*env)->callobjectmethod (env, context, tem_method);//GetpackagenameTem_method = (*env)->getmethodid (env, Class_context,"Getpackagename","() ljava/lang/string;"); Jobject obj_package_name = (*env)->callobjectmethod (env, context, tem_method);//GetpackageinfoTem_class = (*env)->getobjectclass (env, Obj_package_manager); Tem_method = (*env)->getmethodid (env, Tem_class,"Getpackageinfo","(ljava/lang/string;i) landroid/content/pm/packageinfo;"); Jobject obj_package_info = (*env)->callobjectmethod (env, Obj_package_manager, Tem_method, Obj_package_name, -);//signature[] arrayofsignature = localpackageinfo.signatures; //Signature localsignature = arrayofsignature[0];Tem_class = (*env)->getobjectclass (env, obj_package_info); Jfieldid fieldid_signatures = (*env)->getfieldid (env, Tem_class,"Signatures","[Landroid/content/pm/signature;]); Jobjectarray signatures = (*env)->getobjectfield (env, Obj_package_info, fieldid_signatures); Jobject signature = (*env)->getobjectarrayelement (env, signatures,0);//Localsignature.tobytearray ()Tem_class = (*env)->getobjectclass (env, signature); Tem_method = (*env)->getmethodid (env, Tem_class,"Tobytearray","() [B]); Jobject Obj_sign_byte_array = (*env)->callobjectmethod (env, signature, Tem_method);//This is the signature you got. Byte array //MessageDigest localmessagedigest = messagedigest.getinstance ("MD5");Jclass class_messagedigest = (*env)->findclass (env,"Java/security/messagedigest"); Tem_method = (*env)->getstaticmethodid (env, Class_messagedigest,"GetInstance","(ljava/lang/string;) ljava/security/messagedigest;"); Jobject obj_md5 = (*env)->callstaticobjectmethod (env, Class_messagedigest, Tem_method, (*env)->NewStringUTF ( Env"MD5"));//Localmessagedigest.update (Localsignature.tobytearray ()); //tem_class = (*env)->getobjectclass (env, OBJ_MD5);Tem_method = (*env)->getmethodid (env, Class_messagedigest,"Update","([B) V");//The return value of this function is void, write V(*env)->callvoidmethod (env, OBJ_MD5, Tem_method, Obj_sign_byte_array);//Localmessagedigest.digest ()Tem_method = (*env)->getmethodid (env, Class_messagedigest,"Digest","() [B]);//This is a byte array after MD5, now as long as it is converted to 16 binary string, you can compare with the previousJobject obj_array_sign = (*env)->callobjectmethod (env, OBJ_MD5, Tem_method);//Is there a concept of strong turn type in JNI? ////This is the MD5 value of the signature //String str2 = Tohex (Localmessagedigest.digest ()); //Try writing in C: http://blog.csdn.net/pingd/article/details/41945417Jsize int_array_length = (*env)->getarraylength (env, obj_array_sign); jbyte* byte_array_elements = (*env)->getbytearrayelements (env, obj_array_sign, jni_false);Char* Char_result = (Char*)malloc(int_array_length*2+1);//Start without + 1, in some cases will cross-border problems, or in the back of the better. //Convert byte array to 16 binary string, find there is no strong turn, jbyte and unsigned char should be the same number of bytesBytetohexstr (byte_array_elements, Char_result, int_array_length); * (char_result+int_array_length*2) =' + ';//Fill in at the endJstring String_result = (*env)->newstringutf (env, Char_result);//Release(*env)->releasebytearrayelements (env, Obj_array_sign, byte_array_elements, Jni_abort);//release pointer using free Free(Char_result);returnString_result;//If you use Java 16 binary conversion, it should also be possible, write with C 16 binary conversion too troublesome //jclass class_handledata = (*env)->findclass (env, "Com/hengbao/util/handledata"); //tem_method = (*env)->getstaticmethodid (env, Class_handledata, "BytesToHexString1", "([B) ljava/lang/string;"); //jobject Obj_result = (*env)->callstaticobjectmethod (env, Class_handledata, Tem_method, obj_array_sign); //return Obj_result;}
Call Java method in JNI to get the signature file MD5 value of the current apk