如何使本机库返回sha1 app键

时间:2015-03-04 10:26:14

标签: android java-native-interface native

我需要本地库,计算sha1 key并将其返回给应用程序。

我也尝试:Protection Android of applications against breaking

但是我无法编译成*。

也许有人有本地人的经验,并会帮助我。

1 个答案:

答案 0 :(得分:1)

jstring find(JNIEnv *env, jobject thiz, jobject contexObject) 
{
    jclass contextTemp = env->FindClass("android/content/Context");
    jclass contextClass = (jclass)env->NewGlobalRef(contextTemp);
    jmethodID getPackageManager = env->GetMethodID(contextClass, 
    "getPackageManager",
                                                    "()Landroid/content/pm/PackageManager;");
jobject share = (jobject)env->CallObjectMethod(contexObject, getPackageManager);

jmethodID getPackageName = env->GetMethodID(contextClass, "getPackageName",
                                               "()Ljava/lang/String;");
jstring packageName = (jstring)env->CallObjectMethod(contexObject, getPackageName);
jclass clazz = env->FindClass("android/content/pm/PackageManager");

jfieldID GET_SIGNATURESfid = env->GetStaticFieldID( clazz, "GET_SIGNATURES", "I");
jint GET_SIGNATURES = env->GetStaticIntField(clazz,GET_SIGNATURESfid);
//jclass temp = env->FindClass("android/content/pm/PackageManager");
jclass PackageManagerObj = (jclass)env->NewGlobalRef(clazz);
jmethodID getPackageInfoId = env->GetMethodID(PackageManagerObj, "getPackageInfo",
                                               "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
jobject getPackageInfoObject = (jobject)env->CallObjectMethod(share, getPackageInfoId,
                                            packageName,GET_SIGNATURES);
jclass packageInfoClass = env->FindClass("android/content/pm/PackageInfo");
jfieldID signatures = env->GetFieldID(packageInfoClass,"signatures","[Landroid/content/pm/Signature;");
jobjectArray rows = reinterpret_cast<jobjectArray>(
        env->GetObjectField(getPackageInfoObject,signatures));
int size = env->GetArrayLength(rows);
jclass signaturesClass = env->FindClass("android/content/pm/Signature");
jclass  mEssage = env->FindClass("java/security/MessageDigest");
jclass  MessageDigestClass = (jclass)env->NewGlobalRef(mEssage);
jmethodID mGetInstance = env->GetStaticMethodID(MessageDigestClass,"getInstance","(Ljava/lang/String;)Ljava/security/MessageDigest;");
jmethodID updateMethod = env->GetMethodID(MessageDigestClass,"update","([B)V");
jmethodID toByteArrayMethod = env->GetMethodID(signaturesClass,"toByteArray","()[B");

jclass tempBase64 = env->FindClass("android/util/Base64");
jclass Base64Class = (jclass)env->NewGlobalRef(tempBase64);

jmethodID encodeToStringID = env->GetStaticMethodID(Base64Class,"encodeToString","([BI)Ljava/lang/String;");
jmethodID digest = env->GetMethodID(MessageDigestClass,"digest","()[B");
for(int i = 0 ;i<size;i++){
    jobject messageDigest = env->CallStaticObjectMethod(MessageDigestClass,mGetInstance,env->NewStringUTF("SHA"));
    jobject signature = env->GetObjectArrayElement(rows,i);
    env->CallVoidMethod(messageDigest,updateMethod,(jbyteArray)env->CallObjectMethod(signature,toByteArrayMethod));
    return (jstring)env->CallStaticObjectMethod(Base64Class,encodeToStringID,env->CallObjectMethod(messageDigest,digest),(jint)0);
}
return env->NewStringUTF("");

如果你愿意,非常疯狂......