Question

How to implement in JNI this function ?

public byte[] decrypt(byte[] enc) throws Exception{
    Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    SecretKeySpec    myKey = new SecretKeySpec(key, "DESede");
    IvParameterSpec ivspec = new IvParameterSpec(initializationVector);
    c3des.init(Cipher.DECRYPT_MODE, myKey, ivspec);
    byte[] cipherText = c3des.doFinal(enc);
    return cipherText;
}

I wrote almost all of the method, but I have a problem with the two last lines:

jbyteArray Java_MainActivity_decrypt(JNIEnv* env, jobject context,jbyteArray key, jbyteArray iv, jbyteArray enc) {


   //Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
   jclass cl = (*env)->FindClass(env,"javax/crypto/Cipher");
   jmethodID MID = (*env)->GetStaticMethodID(env,cl, "getInstance", "(Ljava/lang/String;)Ljavax/crypto/Cipher;");
   jstring s = (*env)->NewStringUTF(env,"DESede/CBC/PKCS5Padding");
   jobject c3des = (*env)->CallStaticObjectMethod(env,cl, MID, s);

   //SecretKeySpec    myKey = new SecretKeySpec(key, "DESede");
   jclass cl1 = (*env)->FindClass(env, "javax/crypto/spec/SecretKeySpec");
   jclass constructor1 = (*env)->GetMethodID(env, cl1, "<init>", "([BLjava/lang/String;)V");
   jstring s1 = (*env)->NewStringUTF(env,"DESede");
   jobject myKey = (*env)->NewObject(env, cl1, constructor1, key, s1);

   //IvParameterSpec ivspec = new IvParameterSpec(initializationVector);
   jclass cl2 = (*env)->FindClass(env, "javax/crypto/spec/IvParameterSpec");
   jclass constructor2 = (*env)->GetMethodID(env, cl2, "<init>", "([B)V");
   jobject ivspec = (*env)->NewObject(env, cl2, constructor2, iv);

   //c3des.init(Cipher.DECRYPT_MODE, myKey, ivspec);
   jmethodID mid_int = (*env)->GetMethodID(env, cl, "init","(ILjava/security/Key;Ljava/security/AlgorithmParameters;)V");
   jfieldID field_dec_id = (*env)->GetStaticFieldID(env, cl, "DECRYPT_MODE","I");
   jint field_dec = (*env)->GetStaticIntField(env, cl, field_dec_id);
   (*env)->CallVoidMethod(env,c3des,mid_int,field_dec,myKey,ivspec); //<--app crash at this line

   return;
}

app crash at

(*env)->CallVoidMethod(env,c3des,mid_int,field_dec,myKey,ivspec);

myKey and ivspec are ok, I can return them and decrypt in java:

Cipher c3des = Cipher.getInstance("DESede/CBC/NoPadding");
c3des.init(Cipher.DECRYPT_MODE, myKey, ivspec);
byte[] cipherText = c3des.doFinal(enc);

thanks in advance

Était-ce utile?

La solution

You should enable the extended JNI checking by following the instructions at the Android JNI tips page (i.e., adb shell setprop debug.checkjni 1 ). Or you can run your code on the emulator which already has the CheckJNI facility enabled. Typically, after enabling that, looking at the logcat output will give you the exact reason a JNI call is crashing.

Also, between invocations of Java methods you should do:

if (env->ExceptionCheck()) {
    // Optionally log something here.
    return NULL;
}

In this specific case, it appears you're using the wrong function prototype for Cipher#init. IvParameterSpec is an instance of AlgorithmParameterSpec and not AlgorithmParameters, so you need to use the prototype (ILjava/security/Key;Ljava/security/spec/AlgorithmParameterSpec;)V instead.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top