NDK开发(五) :JNI实现文件加解密
2023-06-13 09:15:59 时间
转载请以链接形式标明出处: 本文出自:103style的博客
本文操作以 Android Studio 3.4.2 版本为例
- NDK开发(一) :NDK入门指南
- NDK开发(二) :JNI的数据类型
- NDK开发(三) :JNI访问Java变量和方法
- NDK开发(四) :JNI操作Java数组
- NDK开发(五) :JNI实现文件加解密
- NDK开发(六) :JNI实现文件拆分和合并
目录
- 编写测试代码
- 实现创建文件逻辑
- 实现JNI加密逻辑
- 实现JNI解密逻辑
- 执行测试代码
编写测试代码
创建Encryptor
类,编写对应的测试代码:
public class Encryptor {
private static final String TAG = "Encryptor";
static {
System.loadLibrary("encryptor");
}
private final String BASE_URL = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
/**
* 加密
*
* @param normalPath 文件路径
* @param encryptPath 加密之后的文件路径
*/
public native static void encryption(String normalPath, String encryptPath);
/**
* 解密
*
* @param encryptPath 加密之后的文件路径
* @param decryptPath 解密之后的文件路径
*/
public native static void decryption(String encryptPath, String decryptPath);
/**
* 创建文件
*
* @param normalPath 文件路径
*/
private native void createFile(String normalPath);
/**
* 测试加解密
*/
public void test() {
String fileName = "testJni.txt";
String encryptPath = encryption(fileName);
decryption(encryptPath);
}
/**
* 加密
*/
public String encryption(String fileName) {
String normalPath = BASE_URL + fileName;
File file = new File(normalPath);
if (!file.exists()) {
createFile(normalPath);
}
String encryptPath = BASE_URL + "encryption_" + fileName;
Encryptor.encryption(normalPath, encryptPath);
Log.d(TAG, "加密完成了...");
return encryptPath;
}
/**
* 解密
*/
public void decryption(String encryptPath) {
if (!new File(encryptPath).exists()) {
Log.d(TAG, "解密文件不存在");
return;
}
String decryptPath = encryptPath.replace("encryption_", "decryption_");
Encryptor.decryption(encryptPath, decryptPath);
Log.d(TAG, "解密完成了...");
}
}
创建encryptor.cpp
添加以下代码到CMakeLists.txt
:
add_library(encryptor
SHARED
src/main/cpp/encryptor.cpp)
target_link_libraries(
encryptor
${log-lib})
实现创建文件逻辑
#include <cstdio>
extern "C"
JNIEXPORT void JNICALL
Java_com_example_myapplication_Encryptor_createFile(JNIEnv *env, jobject instance,
jstring normalPath_) {
//获取字符串保存在JVM中内存中
const char *normalPath = env->GetStringUTFChars(normalPath_, nullptr);
//打开 normalPath wb:只写打开或新建一个二进制文件;只允许写数据
FILE *fp = fopen(normalPath, "wb");
//把字符串写入到指定的流 stream 中,但不包括空字符。
fputs("Hi, this file is created by JNI, and my name is 103style.", fp);
//关闭流 fp。刷新所有的缓冲区
fclose(fp);
//释放JVM保存的字符串的内存
env->ReleaseStringUTFChars(normalPath_, normalPath);
}
实现JNI加密逻辑
#include <android/log.h>
#include <cstdio>
#include <cstring>
#define LOGE(FORMAT, ...) __android_log_print(ANDROID_LOG_ERROR,"Encryptor",FORMAT,##__VA_ARGS__);
char password[] = "103style";
extern "C"
JNIEXPORT void JNICALL
Java_com_example_myapplication_Encryptor_encryption(JNIEnv *env, jclass type, jstring normalPath_,
jstring encryptPath_) {
//获取字符串保存在JVM中内存中
const char *normalPath = env->GetStringUTFChars(normalPath_, nullptr);
const char *encryptPath = env->GetStringUTFChars(encryptPath_, nullptr);
LOGE("normalPath = %s, encryptPath = %s", normalPath, encryptPath);
//rb:只读打开一个二进制文件,允许读数据。
//wb:只写打开或新建一个二进制文件;只允许写数据
FILE *normal_fp = fopen(normalPath, "rb");
FILE *encrypt_fp = fopen(encryptPath, "wb");
if (normal_fp == nullptr) {
LOGE("%s", "文件打开失败");
return;
}
//一次读取一个字符
int ch = 0;
int i = 0;
size_t pwd_length = strlen(password);
while ((ch = fgetc(normal_fp)) != EOF) { //End of File
//写入(异或运算)
fputc(ch ^ password[i % pwd_length], encrypt_fp);
i++;
}
//关闭流 normal_fp和encrypt_fp。刷新所有的缓冲区
fclose(normal_fp);
fclose(encrypt_fp);
//释放JVM保存的字符串的内存
env->ReleaseStringUTFChars(normalPath_, normalPath);
env->ReleaseStringUTFChars(encryptPath_, encryptPath);
}
实现JNI解密逻辑
#include <android/log.h>
#include <cstdio>
#include <cstring>
#define LOGE(FORMAT, ...) __android_log_print(ANDROID_LOG_ERROR,"Encryptor",FORMAT,##__VA_ARGS__);
char password[] = "103style";
extern "C"
JNIEXPORT void JNICALL
Java_com_example_myapplication_Encryptor_decryption(JNIEnv *env, jclass type, jstring encryptPath_,
jstring decryptPath_) {
获取字符串保存在JVM中内存中
const char *encryptPath = env->GetStringUTFChars(encryptPath_, nullptr);
const char *decryptPath = env->GetStringUTFChars(decryptPath_, nullptr);
LOGE("encryptPath = %s, decryptPath = %s", encryptPath, decryptPath);
//rb:只读打开一个二进制文件,允许读数据。
//wb:只写打开或新建一个二进制文件;只允许写数据
FILE *encrypt_fp = fopen(encryptPath, "rb");
FILE *decrypt_fp = fopen(decryptPath, "wb");
if (encrypt_fp == nullptr) {
LOGE("%s", "加密文件打开失败");
return;
}
int ch;
int i = 0;
size_t pwd_length = strlen(password);
while ((ch = fgetc(encrypt_fp)) != EOF) {
fputc(ch ^ password[i % pwd_length], decrypt_fp);
i++;
}
//关闭流 encrypt_fp 和 decrypt_fp。刷新所有的缓冲区
fclose(encrypt_fp);
fclose(decrypt_fp);
//释放JVM保存的字符串的内存
env->ReleaseStringUTFChars(encryptPath_, encryptPath);
env->ReleaseStringUTFChars(decryptPath_, decryptPath);
}
执行测试代码
private void testEncryptor() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0x1024);
return;
}
}
new Encryptor().test();
}
执行程序会在 手机根目录 生成以下三个文件:
testJni.txt
:原文件encryption_testJni.txt
:加密之后的文件decryption_testJni.txt
:解密之后的文件
demo地址: https://github.com/103style/NDKDoc/tree/master/HelloNDK
以上
相关文章
- Java 读写文件工具类
- 在线客服系统源码开发实战总结:动态加载js文件实现粘贴一段js的sdk代码,直接引入插件效果
- 【Android NDK 开发】NDK C/C++ 代码崩溃调试 - Tombstone 报错信息日志文件分析 ( 获取 tombstone_0X 崩溃日志信息 )
- 【Android 安全】DEX 加密 ( 代理 Application 开发 | 解压 apk 文件 | 判定是否是第一次启动 | 递归删除文件操作 | 解压 Zip 文件操作 )
- iOS开发之基础篇(1)—— 证书、打包上架流程、p12文件
- 数据库开发知识:SpringBoot 怎么集成MongoDB实现文件上传功能
- iOS常见文件及程序的启动原理详解手机开发
- IOS网络编程—(数据请求+slider)将网络上的大文件下载到本地,并打印其进度详解手机开发
- [android] 保存文件到手机内存详解手机开发
- 文件图片上传详解手机开发
- iOS项目生成静态库文件(.a)详解手机开发
- 管理Linux文件的用户权限方法(linux文件用户权限)
- 文件利用 Linux 命令行创建文本文件(linux创建文本)
- 文件阅读Oracle中文CHM文件精彩阅读(oracle中文chm)
- Linux查看文件缓存:简单而实用的方法(linux查看文件缓存)
- Linux系统文件重命名实战指南(linux文件重命名命令)
- Oracle PDE文件:统一的开发解决方案(oraclepde文件)
- Bash 中的 & 符号和文件描述符
- 处理Linux文本文件处理之道(linuxtxt文件)
- Linux如何查看文件路径大小(linux查看路径大小)
- ASP开发中数据库文件调用的捷径
- android开发基础教程—文件存储功能实现
- 如何调试异步加载页面里包含的js文件
- node.js开发中使用NodeSupervisor实现监测文件修改并自动重启应用