jobject getApplication(JNIEnv *env) { jclass localClass = env->FindClass("android/app/ActivityThread"); if (localClass != NULL) { jmethodID getapplication = env->GetStaticMethodID(localClass, "currentApplication", "()Landroid/app/Application;"); if (getapplication != NULL) { jobject application = env->CallStaticObjectMethod(localClass, getapplication); return application; } return NULL; } return NULL; }
void checkSign(JNIEnv *env ){ jobject context= getApplication(env); jclass activity = env->GetObjectClass(context); // 得到 getPackageManager 方法的 ID jmethodID methodID_func = env->GetMethodID(activity, "getPackageManager", "()Landroid/content/pm/PackageManager;"); // 获得PackageManager对象 jobject packageManager = env->CallObjectMethod(context,methodID_func); jclass packageManagerclass = env->GetObjectClass(packageManager); //得到 getPackageName 方法的 ID jmethodID methodID_pack = env->GetMethodID(activity,"getPackageName", "()Ljava/lang/String;"); //获取包名 jstring name_str = static_cast<jstring>(env->CallObjectMethod(context, methodID_pack)); // 得到 getPackageInfo 方法的 ID jmethodID methodID_pm = env->GetMethodID(packageManagerclass,"getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;"); // 获得应用包的信息 jobject package_info = env->CallObjectMethod(packageManager, methodID_pm, name_str, 64); // 获得 PackageInfo 类 jclass package_infoclass = env->GetObjectClass(package_info); // 获得签名数组属性的 ID jfieldID fieldID_signatures = env->GetFieldID(package_infoclass,"signatures", "[Landroid/content/pm/Signature;"); // 得到签名数组,待修改 jobject signatur = env->GetObjectField(package_info, fieldID_signatures); jobjectArray signatures = reinterpret_cast<jobjectArray>(signatur); jobject signature = env->GetObjectArrayElement(signatures, 0); // localSignature.toByteArray() jclass tem_class; jmethodID tem_method; tem_class = env->GetObjectClass(signature); tem_method = env->GetMethodID(tem_class, "toByteArray", "()[B"); jobject obj_sign_byte_array = env->CallObjectMethod(signature, tem_method);// 这个就是拿到的签名byte数组 char *pmsg = ConvertJByteaArrayToChars(env, static_cast<jbyteArray>(obj_sign_byte_array)); int len = env->GetArrayLength(static_cast<jbyteArray>(obj_sign_byte_array)); string hexstr = dump_block(pmsg,len); LOGI("self toByteArray:len %d %s ",hexstr.length(), hexstr.c_str() ); }
char* ConvertJByteaArrayToChars(JNIEnv *env, jbyteArray bytearray) { char *chars = NULL; jbyte *bytes; bytes = env->GetByteArrayElements(bytearray, 0); int chars_len = env->GetArrayLength(bytearray); chars = new char[chars_len + 1]; memset(chars,0,chars_len + 1); memcpy(chars, bytes, chars_len); chars[chars_len] = 0; env->ReleaseByteArrayElements(bytearray, bytes, 0); return chars; }
string dump_block(const char* aInStr,int len) { const char HEX[] = "0123456789ABCDEF"; std::string lStr; for (int i = 0; i < len; i++) { char lTemp = aInStr[i]; lStr.push_back(HEX[lTemp / 16]); lStr.push_back(HEX[lTemp % 16]); } return lStr; }