jstring getPublicKey(JNIEnv* env, jobject thiz,jobject context) { jclass context_cls = env->GetObjectClass(context);
jmethodID get_package_manager_mid = env->GetMethodID(context_cls, "getPackageManager", "()Landroid/content/pm/PackageManager;");
jmethodID get_package_name_mid = env->GetMethodID(context_cls, "getPackageName", "()Ljava/lang/String;"); env->DeleteLocalRef(context_cls); jobject pm_obj = env->CallObjectMethod(context, get_package_manager_mid); jclass pm_cls = env->FindClass("android/content/pm/PackageManager");
jmethodID get_package_info_mid = env->GetMethodID(pm_cls, "getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;"); jstring package_name = reinterpret_cast<jstring>(env->CallObjectMethod( context, get_package_name_mid)); jfieldID flag_fid = env->GetStaticFieldID(pm_cls, "GET_SIGNATURES", "I"); jint flag = env->GetStaticIntField(pm_cls, flag_fid); env->DeleteLocalRef(pm_cls); jobject pi_obj = env->CallObjectMethod(pm_obj, get_package_info_mid, package_name, flag); env->DeleteLocalRef(package_name);
jclass pi_cls = env->FindClass("android/content/pm/PackageInfo"); jfieldID signatures_fid = env->GetFieldID(pi_cls, "signatures", "[Landroid/content/pm/Signature;"); env->DeleteLocalRef(pi_cls); jobject sig_obj = env->GetObjectField(pi_obj, signatures_fid); env->DeleteLocalRef(pi_obj);
jobjectArray sigs = reinterpret_cast<jobjectArray>(sig_obj);
jclass signature_cls = env->FindClass("android/content/pm/Signature"); jmethodID to_byte_array_mid = env->GetMethodID(signature_cls, "toByteArray", "()[B");
jbyteArray sig_bytes = reinterpret_cast<jbyteArray>(env->CallObjectMethod( env->GetObjectArrayElement(sigs, 0), to_byte_array_mid)); jclass certificate_factory_cls = env->FindClass( "java/security/cert/CertificateFactory"); jmethodID get_certificate_instance_mid = env->GetStaticMethodID( certificate_factory_cls, "getInstance", "(Ljava/lang/String;)Ljava/security/cert/CertificateFactory;");
jobject certificate_factory_obj = env->CallStaticObjectMethod( certificate_factory_cls, get_certificate_instance_mid, env->NewStringUTF("X509")); jmethodID generate_certificate_mid = env->GetMethodID( certificate_factory_cls, "generateCertificate", "(Ljava/io/InputStream;)Ljava/security/cert/Certificate;"); env->DeleteLocalRef(certificate_factory_cls);
jclass certificate_cls = env->FindClass("java/security/cert/Certificate"); jclass byte_input_stream_cls = env->FindClass( "java/io/ByteArrayInputStream"); jmethodID new_sig_bytes_is_mid = env->GetMethodID(byte_input_stream_cls, "<init>", "([B)V"); jobject sig_bytes_is = env->NewObject(byte_input_stream_cls, new_sig_bytes_is_mid, sig_bytes); env->DeleteLocalRef(sig_bytes); env->DeleteLocalRef(byte_input_stream_cls); jobject cert = env->CallObjectMethod(certificate_factory_obj, generate_certificate_mid, sig_bytes_is); env->DeleteLocalRef(sig_bytes_is); env->DeleteLocalRef(certificate_factory_obj); jmethodID get_pubic_key_mid = env->GetMethodID(certificate_cls, "getPublicKey", "()Ljava/security/PublicKey;"); env->DeleteLocalRef(certificate_cls);
jobject publicKey = env->CallObjectMethod(cert, get_pubic_key_mid); jclass publicKey_cls = env->GetObjectClass(publicKey); jmethodID toString_mid = env->GetMethodID(publicKey_cls,"toString", "()Ljava/lang/String;"); jstring publicKey_str = static_cast<jstring>(env->CallObjectMethod(publicKey,toString_mid)); env->DeleteLocalRef(cert); env->DeleteLocalRef(publicKey_cls); env->DeleteLocalRef(publicKey);
jclass string_cls = env->GetObjectClass(publicKey_str); jmethodID indexOf_mid = env->GetMethodID(string_cls,"indexOf", "(Ljava/lang/String;)I"); jstring param = env->NewStringUTF("modulus"); jint aa = env->CallIntMethod(publicKey_str,indexOf_mid,param); jstring param2 = env->NewStringUTF("publicExponent");
jmethodID substring_mid = env->GetMethodID(string_cls,"substring", "(II)Ljava/lang/String;");
__android_log_print(ANDROID_LOG_ERROR, TAG, "PublicKey String is %s", jstringTostring(env,publicKey_str));
jint bb = env->CallIntMethod(publicKey_str,indexOf_mid,param2); jstring publicKey2_str =NULL; if (bb ==-1){ jstring param3 = env->NewStringUTF("public exponent"); jint cc = env->CallIntMethod(publicKey_str,indexOf_mid,param3); __android_log_print(ANDROID_LOG_ERROR, TAG, "CC value is %d", cc); publicKey2_str= static_cast<jstring>(env->CallObjectMethod(publicKey_str,substring_mid,aa+8,cc-1)); }else { publicKey2_str = static_cast<jstring>(env->CallObjectMethod(publicKey_str,substring_mid,aa+8,bb-1)); }
__android_log_print(ANDROID_LOG_ERROR, TAG, "PublicKey value is %s", jstringTostring(env,publicKey2_str)); return publicKey2_str; }
|