• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Android App开发:JNI实现加密和解密操作附源码

武飞扬头像
showswoller
帮助1

运行有问题或需要源码请点赞关注收藏后评论区留言~~~

一、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类

  1.  
    package com.example.ebook;
  2.  
     
  3.  
    import android.os.Bundle;
  4.  
    import android.text.TextUtils;
  5.  
    import android.widget.EditText;
  6.  
    import android.widget.TextView;
  7.  
    import android.widget.Toast;
  8.  
     
  9.  
    import androidx.appcompat.app.AppCompatActivity;
  10.  
     
  11.  
    public class JniSecretActivity extends AppCompatActivity {
  12.  
    private EditText et_origin; // 声明一个用于输入原始字符串的编辑框对象
  13.  
    private TextView tv_encrypt; // 声明一个文本视图对象
  14.  
    private TextView tv_decrypt; // 声明一个文本视图对象
  15.  
    private String mKey = "123456789abcdef"; // 该算法要求密钥串的长度为16
  16.  
    private String mEncrypt; // 加密串
  17.  
     
  18.  
    @Override
  19.  
    protected void onCreate(Bundle savedInstanceState) {
  20.  
    super.onCreate(savedInstanceState);
  21.  
    setContentView(R.layout.activity_jni_secret);
  22.  
    et_origin = findViewById(R.id.et_origin);
  23.  
    tv_encrypt = findViewById(R.id.tv_encrypt);
  24.  
    tv_decrypt = findViewById(R.id.tv_decrypt);
  25.  
    findViewById(R.id.btn_encrypt).setOnClickListener(v -> {
  26.  
    // 调用JNI方法encryptFromJNI获得加密后的字符串
  27.  
    mEncrypt = encryptFromJNI(et_origin.getText().toString(), mKey);
  28.  
    tv_encrypt.setText("jni加密结果为:" mEncrypt);
  29.  
    });
  30.  
    findViewById(R.id.btn_decrypt).setOnClickListener(v -> {
  31.  
    if (TextUtils.isEmpty(mEncrypt)) {
  32.  
    Toast.makeText(this, "请先加密后再解密", Toast.LENGTH_SHORT).show();
  33.  
    return;
  34.  
    }
  35.  
    // 调用JNI方法decryptFromJNI获得解密后的字符串
  36.  
    String raw = decryptFromJNI(mEncrypt, mKey);
  37.  
    tv_decrypt.setText("jni解密结果为:" raw);
  38.  
    });
  39.  
    }
  40.  
     
  41.  
    // 声明encryptFromJNI是来自于JNI的原生方法
  42.  
    public native String encryptFromJNI(String raw, String key);
  43.  
     
  44.  
    // 声明decryptFromJNI是来自于JNI的原生方法
  45.  
    public native String decryptFromJNI(String des, String key);
  46.  
     
  47.  
    // 在加载当前类时就去加载libcommon.so,加载动作发生在页面启动之前
  48.  
    static {
  49.  
    System.loadLibrary("common");
  50.  
    }
  51.  
     
  52.  
    }
学新通

XML文件

  1.  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.  
    android:layout_width="match_parent"
  3.  
    android:layout_height="wrap_content"
  4.  
    android:focusable="true"
  5.  
    android:focusableInTouchMode="true"
  6.  
    android:orientation="vertical"
  7.  
    android:padding="5dp" >
  8.  
     
  9.  
    <EditText
  10.  
    android:id="@ id/et_origin"
  11.  
    android:layout_width="match_parent"
  12.  
    android:layout_height="wrap_content"
  13.  
    android:padding="5dp"
  14.  
    android:background="@drawable/editext_selector"
  15.  
    android:inputType="text"
  16.  
    android:hint="请输入待加密的字符串"
  17.  
    android:textColor="@color/black"
  18.  
    android:textSize="17sp" />
  19.  
     
  20.  
    <Button
  21.  
    android:id="@ id/btn_encrypt"
  22.  
    android:layout_width="match_parent"
  23.  
    android:layout_height="wrap_content"
  24.  
    android:gravity="center"
  25.  
    android:text="调用jni接口获取加密串"
  26.  
    android:textColor="@color/black"
  27.  
    android:textSize="17sp" />
  28.  
     
  29.  
    <TextView
  30.  
    android:id="@ id/tv_encrypt"
  31.  
    android:layout_width="match_parent"
  32.  
    android:layout_height="wrap_content"
  33.  
    android:text="这里显示jni加密结果"
  34.  
    android:textColor="@color/black"
  35.  
    android:textSize="17sp" />
  36.  
     
  37.  
    <Button
  38.  
    android:id="@ id/btn_decrypt"
  39.  
    android:layout_width="match_parent"
  40.  
    android:layout_height="wrap_content"
  41.  
    android:gravity="center"
  42.  
    android:text="调用jni接口获取解密串"
  43.  
    android:textColor="@color/black"
  44.  
    android:textSize="17sp" />
  45.  
     
  46.  
    <TextView
  47.  
    android:id="@ id/tv_decrypt"
  48.  
    android:layout_width="match_parent"
  49.  
    android:layout_height="wrap_content"
  50.  
    android:text="这里显示jni解密结果"
  51.  
    android:textColor="@color/black"
  52.  
    android:textSize="17sp" />
  53.  
     
  54.  
    </LinearLayout>
学新通

创作不易 觉得有帮助请点赞关注收藏~~~

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgeiekc
系列文章
更多 icon
同类精品
更多 icon
继续加载