安卓NDK开发案列一:字符串的加密和解密
来源:互联网 发布:财达大智慧软件下载 编辑:程序博客网 时间:2024/06/14 14:43
前言:通过此案例来加深一下我们对JNI字符串的处理,当然字符串的操作用java也可以完美解决,废话不多说开始今天的撸代码!
--------分割线--------
ok,我们来看一下效果图:
实现思路:
1.从edittext获取字符串。
2.将获得的字符串传递给jni
3.在jni进行字符串的加密,然后返回。
4.在jni进行字符串的解密,然后返回。
------------分割线-------
java代码:
MainActivity.java:
package com.fly.demo5;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.EditText;public class MainActivity extends AppCompatActivity { private EditText et; private JNI jni; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et = (EditText) findViewById(R.id.et); jni = new JNI(); } public void encode(View view) { //获取edittext的文字 String word = et.getText().toString(); //传递c加密处理 word = jni.encodeInC(word); //把处理后的结果显示到edittext上 et.setText(word); } public void decode(View view) { //获取edittext的文字 String word = et.getText().toString(); //传递c加密处理 word = jni.decodeInC(word); //把处理后的结果显示到edittext上 et.setText(word); }}JNI.java:
package com.fly.demo5;/** * Created by Fly on 2017/7/6. */public class JNI { static { System.loadLibrary("native-lib"); } public native String encodeInC(String word); public native String decodeInC(String word);}简单的布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.fly.demo5.MainActivity"> <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="encode" android:text="加密" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="decode" android:text="解密" /> </LinearLayout></LinearLayout>C代码:
//// Created by Fly on 2017/7/6.//#include <jni.h>#include <stdlib.h>#include <stdio.h>/** * 把一个jstring转换成一个c语言的char* 类型. */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_fly_demo5_JNI_encodeInC (JNIEnv *env, jobject clazz, jstring jstr) {//把jstring类型字符串转化成 c能够处理的char* 类型 char *cstr = _JString2CStr(env, jstr); int length = strlen(cstr); int i; for (i = 0; i < length; i++) { *(cstr + i) += 2; } return (*env)->NewStringUTF(env, cstr);}/* * Class: com_fly_demo5_JNI * Method: decodeInC * Signature: (Ljava/lang/String;)Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_fly_demo5_JNI_decodeInC (JNIEnv *env, jobject clazz, jstring jstr) { char *cstr = _JString2CStr(env, jstr); int length = strlen(cstr); int i; for (i = 0; i < length; i++) { *(cstr + i) -= 2; } return (*env)->NewStringUTF(env, cstr);}--------完--------
阅读全文
1 0
- 安卓NDK开发案列一:字符串的加密和解密
- 字符串的加密和解密
- 字符串的加密和解密
- 加密和解密字符串。
- 连接字符串的加密和解密
- 常用的字符串加密和解密
- Java 字符串的加密和解密
- java 字符串MD5的加密和解密
- 字符串的加密解密
- 【加密解密】对字符串的加密解密
- 解决linux c利用openssl进行3des和安卓封装的接口加密解密出的字符串不一样的问题
- 安卓AES加密解密
- 安卓DES加密解密
- 安卓RSA加密解密
- C#加密和解密字符串
- C#字符串加密和解密
- C#字符串加密和解密
- discuz的DES加密字符串和解密字符串
- Max OS 安装go (解决can't load package)
- 如何异步处理繁重的业务
- 分析:手机端护照识别sdk
- ConfUtil 读取配置文件工具类
- vue移动端模仿qq左右滑动删除
- 安卓NDK开发案列一:字符串的加密和解密
- effective c++条款总结
- 数据仓库学习笔记01
- BZOJ 1007-水平可见直线
- Markdown语言详解
- PAT--1052. Linked List Sorting
- Spring 获取属性值
- WSGI学习记录
- Datacamp-python-Dictionaries