Search code examples
androidjava-native-interface

How to write Cipher.getInstance("DES"); in jni?


I would like to change

Cipher cipher = Cipher.getInstance("DES"); 

into cpp code in jni.How to do it?


Solution

  • JNIEnv *jni; //Comes from somewhere
    jclass cl = jni->FindClass("javax/crypto/Cipher");
    jmethodID MID = jni->GetStaticMethodID(cl, "getInstance", "(Ljava/lang/String)Ljavax/crypto/Cipher;");
    jstring s = jni->NewStringUTF("DES");
    jobject cipher = jni->CallStaticObjectMethod(cl, MID, s);
    

    That's omitting error handling.