Android jni字符串如何加密
2016-06-06 23:06
295 查看
1、创建布局:
2、创建页面
3、进入项目的src目录下
按住shift+右键,打开cmd窗口
在窗口里输入
此时会生成文件com_android_imooc_jni_JniActivity
打开
拷贝里面的两个方法到hello.c文件里
4、改写hello.c里的这两个方法
5、运行,如图
6、输入字母”abc”
得到结果
06-06 14:09:55.561: I/System.out(3956): 加密密码=cde
06-06 14:09:58.437: I/System.out(3956): 解密密码=bcd
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <EditText android:id="@+id/et_pwd" android:layout_width="match_parent" android:layout_height="50dp" android:inputType="textPassword" /> <LinearLayout android:layout_width="match_parent" android:gravity="center_horizontal" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn_encode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="encode" android:text="加密" /> <Button android:id="@+id/btn_decode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="decode" android:text="解密" /> </LinearLayout> </LinearLayout>
2、创建页面
/** * @描述 TODO * @项目名称 App_imooc * @包名 com.android.imooc.jni * @类名 JniActivity * @author chenlin * @date 2013年5月24日 上午6:58:39 * @version 1.0 */ public class JniActivity extends Activity { private EditText mEtPwd; static{ //加载类库 System.loadLibrary("hello"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_jni); mEtPwd = (EditText) findViewById(R.id.et_pwd); Log.i("tag", helloFromC()); } public void encode(View view){ String text = mEtPwd.getText().toString(); if (!TextUtils.isEmpty(text)) { String pwd = encodePwd(text, text.length()); System.out.println("加密密码=" + pwd); mEtPwd.setText(pwd); } } public void decode(View view){ String text = mEtPwd.getText().toString(); if (!TextUtils.isEmpty(text)) { System.out.println("解密密码=" + decodePwd(text, text.length())); } } public native String helloFromC(); public native int sum(int x, int y); public native String encodePwd(String pass, int len); public native String decodePwd(String pass, int len); }
3、进入项目的src目录下
按住shift+右键,打开cmd窗口
在窗口里输入
javah com.android.imooc.jni.JniActivity
此时会生成文件com_android_imooc_jni_JniActivity
打开
拷贝里面的两个方法到hello.c文件里
JNIEXPORT jstring JNICALL Java_com_android_imooc_jni_JniActivity_encodePwd (JNIEnv *, jobject, jstring, jint); JNIEXPORT jstring JNICALL Java_com_android_imooc_jni_JniActivity_decodePwd (JNIEnv *, jobject, jstring, jint);
4、改写hello.c里的这两个方法
//把java的字符串转换成c的字符串 char* Jstring2CStr(JNIEnv* env, jstring jstr) { char* rtn = NULL; jclass clsstring = (*env)->FindClass(env, "java/lang/String"); jstring strencode = (*env)->NewStringUTF(env, "GB2312"); jmethodID mid = (*env)->GetMethodID(env, clsstring, "getBytes", "(Ljava/lang/String;)[B"); jbyteArray barr = (jbyteArray) (*env)->CallObjectMethod(env, jstr, mid, strencode); // String .getByte("GB2312"); jsize alen = (*env)->GetArrayLength(env, barr); jbyte* ba = (*env)->GetByteArrayElements(env, barr, JNI_FALSE); if (alen > 0) { rtn = (char*) malloc(alen + 1); //"\0" memcpy(rtn, ba, alen); rtn[alen] = 0; } (*env)->ReleaseByteArrayElements(env, barr, ba, 0); // return rtn; } //加密 JNIEXPORT jstring JNICALL Java_com_android_imooc_jni_JniActivity_encodePwd( JNIEnv * env, jobject obj, jstring jstr, jint len) { char * cstr = Jstring2CStr(env, jstr); for (int i = 0; i < len; i++) { *(cstr + i) += 1; } return (*env)->NewStringUTF(env, cstr); } //解密 JNIEXPORT jstring JNICALL Java_com_android_imooc_jni_JniActivity_decodePwd( JNIEnv * env, jobject obj, jstring jstr, jint len) { char * cstr = Jstring2CStr(env, jstr); for (int i = 0; i < len; i++) { *(cstr + i) -= 1; } return (*env)->NewStringUTF(env, cstr); }
5、运行,如图
6、输入字母”abc”
得到结果
06-06 14:09:55.561: I/System.out(3956): 加密密码=cde
06-06 14:09:58.437: I/System.out(3956): 解密密码=bcd
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题