Android App开发:JNI实现加密和解密操作附源码
运行有问题或需要源码请点赞关注收藏后评论区留言~~~
一、JNI实现加密和解密
在实际开发中 JNI主要应用于以下场景
1:对关键业务数据进行加密和解密
Java代码容易遭到破解,JNI加密更加安全
2:底层的网络操作与设备操作
Java作为一门高级程序设计语言 与硬件和网络操作的隔阂比C/C 大,它不想它俩那样容易驾驭硬件和网络的操作
3:对运行效率要求较高的场合
同样的操作C/C 执行效率比Java高很多,另外,图像处理,音视频处理等需要大量运算的场合,其底层算法也都是用C/C 实现。
4:跨平台的应用移植
移动设备的操作系统不是Android就是IOS,如果部分业务功能采用C/C 实现,那么不但Android可以通过JNI调用,而且IOS能直接编译运行,一份代码可以同时被两个平台复用,省时省力
接下来尝试使用JNI完成加解密操作,采用的是AES算法C 的开源代码,主要的改造工作是给C 源代码配上JNI接口
效果如下 分别输入原始字符串并调用JNI接口进行加密,并且对已加密的字符串进行JNI解密操作
代码如下
Java类
-
package com.example.ebook;
-
-
import android.os.Bundle;
-
import android.text.TextUtils;
-
import android.widget.EditText;
-
import android.widget.TextView;
-
import android.widget.Toast;
-
-
import androidx.appcompat.app.AppCompatActivity;
-
-
public class JniSecretActivity extends AppCompatActivity {
-
private EditText et_origin; // 声明一个用于输入原始字符串的编辑框对象
-
private TextView tv_encrypt; // 声明一个文本视图对象
-
private TextView tv_decrypt; // 声明一个文本视图对象
-
private String mKey = "123456789abcdef"; // 该算法要求密钥串的长度为16位
-
private String mEncrypt; // 加密串
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_jni_secret);
-
et_origin = findViewById(R.id.et_origin);
-
tv_encrypt = findViewById(R.id.tv_encrypt);
-
tv_decrypt = findViewById(R.id.tv_decrypt);
-
findViewById(R.id.btn_encrypt).setOnClickListener(v -> {
-
// 调用JNI方法encryptFromJNI获得加密后的字符串
-
mEncrypt = encryptFromJNI(et_origin.getText().toString(), mKey);
-
tv_encrypt.setText("jni加密结果为:" mEncrypt);
-
});
-
findViewById(R.id.btn_decrypt).setOnClickListener(v -> {
-
if (TextUtils.isEmpty(mEncrypt)) {
-
Toast.makeText(this, "请先加密后再解密", Toast.LENGTH_SHORT).show();
-
return;
-
}
-
// 调用JNI方法decryptFromJNI获得解密后的字符串
-
String raw = decryptFromJNI(mEncrypt, mKey);
-
tv_decrypt.setText("jni解密结果为:" raw);
-
});
-
}
-
-
// 声明encryptFromJNI是来自于JNI的原生方法
-
public native String encryptFromJNI(String raw, String key);
-
-
// 声明decryptFromJNI是来自于JNI的原生方法
-
public native String decryptFromJNI(String des, String key);
-
-
// 在加载当前类时就去加载libcommon.so,加载动作发生在页面启动之前
-
static {
-
System.loadLibrary("common");
-
}
-
-
}
XML文件
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:focusable="true"
-
android:focusableInTouchMode="true"
-
android:orientation="vertical"
-
android:padding="5dp" >
-
-
<EditText
-
android:id="@ id/et_origin"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:padding="5dp"
-
android:background="@drawable/editext_selector"
-
android:inputType="text"
-
android:hint="请输入待加密的字符串"
-
android:textColor="@color/black"
-
android:textSize="17sp" />
-
-
<Button
-
android:id="@ id/btn_encrypt"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:gravity="center"
-
android:text="调用jni接口获取加密串"
-
android:textColor="@color/black"
-
android:textSize="17sp" />
-
-
<TextView
-
android:id="@ id/tv_encrypt"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:text="这里显示jni加密结果"
-
android:textColor="@color/black"
-
android:textSize="17sp" />
-
-
<Button
-
android:id="@ id/btn_decrypt"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:gravity="center"
-
android:text="调用jni接口获取解密串"
-
android:textColor="@color/black"
-
android:textSize="17sp" />
-
-
<TextView
-
android:id="@ id/tv_decrypt"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:text="这里显示jni解密结果"
-
android:textColor="@color/black"
-
android:textSize="17sp" />
-
-
</LinearLayout>
创作不易 觉得有帮助请点赞关注收藏~~~
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgeiekc
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01