This section learns to use the C language to encrypt strings, and everyone knows that encryption using Java implementations can be deserialized, whereas encryption using C writes is very difficult to decompile. So we use JNI to learn how to use C to implement the encryption of strings.
First: We implement an interface
The layout file is subsidized.
Second: Add a local method of encryption and decryption to the mainactivity
Encrypt local method public native string Encodefromc (string text, int length),//Decrypt local method public native string Decodefromc (string text, I NT length);
When the encrypted button is pressed:
public void Encode (View v) { String passwd = Et.gettext (). toString (); Et.settext (ENCODEFROMC (passwd, Passwd.length ())); }
Display the encrypted characters on the EditText
When the decrypted button is pressed, the characters in the EditText are decrypted. The decrypted character is then displayed
public void decode (View v) { String passwd = Et.gettext (). toString (); Et.settext (DECODEFROMC (passwd, Passwd.length ())); }
Finally: Implementing a Local method
#include <jni.h> #include <string.h>//converts a Java string into a C string, using the Reflection char* Jstring2cstr (jnienv* env, jstring JST R) {char* RTN = NULL;//1: First find bytecode file Jclass clsstring = (*env)->findclass (env, "java/lang/string"); jstring Strencode = (*env)->newstringutf (env, "GB2312"); 2: Find the method ID through the bytecode file jmethodid mid = (*env)->getmethodid (env,clsstring, "GetBytes", "(ljava/lang/string;) [B"); /3: By Method ID, call method jbytearray barr= (Jbytearray) (*env)->callobjectmethod (Env,jstr,mid,strencode); String. GetByte ("GB2312"); 4: Get the length of the data jsize alen = (*env)->getarraylength (Env,barr); 5: Get the first address of the data jbyte* ba = (*env)->getbytearrayelements (Env,barr,jni_false); 6: Get the C language string if (Alen > 0) {rtn = (char*) malloc (alen+1); "memcpy" (Rtn,ba,alen); rtn[alen]=0; } (*env)->releasebytearrayelements (env,barr,ba,0); return RTN;} /* * class:com_demo_passwd_mainactivity * METHOD:ENCODEFROMC * Signature: (ljava/lang/string;i) Ljava/lang/string; */jniexport jstring jnicall JAVA_COM_DEMO_PASSWD_MAINACTIVITY_ENCODEFROMC (jnienv *env, Jobject obj, jstring passwd, Jin T length) {///1: Convert Java string to c language char* CStr = JSTRING2CSTR (env, passwd); int i = 0;//2: give c word multibyte 1for (i = 0; i < length; i++) {* (CStr + i) + = 1;} 3: Convert C string to Java string return (*env)->newstringutf (env, CSTR);} /* * class:com_demo_passwd_mainactivity * METHOD:DECODEFROMC * Signature: (ljava/lang/string;i) ljava/lang/string; */jniexport jstring jnicall JAVA_COM_DEMO_PASSWD_MAINACTIVITY_DECODEFROMC (jnienv *env, Jobject obj, jstring passwd, Jint length) {//1: Convert Java string to c char* CStr = Jstring2cstr (env, passwd); int i = 0;//2: give C language character minus 1for (i = 0; i < length; i++ {* (CStr + i)-= 1;} 3: Convert C string to Java string return (*env)->newstringutf (env, CSTR);}Display effect:
When entering in the input box: ABCDEFG
Decrypt:
Android Learning Jni, using JNI to implement string encryption