ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

NDK开发(二),ndk开发前景

2022-01-28 16:59:06  阅读:216  来源: 互联网

标签:NDK file crypt ndk jstr char 开发 env path


#define LOGE(FORMAT, …) __android_log_print(ANDROID_LOG_ERROR,“huangxiaoguo”,FORMAT,VA_ARGS)

char password[] = “huangxiaoguo”;

//加密

JNIEXPORT void JNICALL Java_cn_tsou_ndkfilecrypt_ndk_Cryptor_crypt

(JNIEnv *env, jclass jcls, jstring normal_path_jstr, jstring crypt_path_jstr) {

//jstring ->char*

const char *normal_path = (*env)->GetStringUTFChars(env, normal_path_jstr, NULL);

const char *crypt_path = (*env)->GetStringUTFChars(env, crypt_path_jstr, NULL);

LOGI(" path1:%s", normal_path);

LOGI(" path2:%s", crypt_path);

//打开文件

FILE *normal_fp = fopen(normal_path, “rb”);

FILE *crypt_fp = fopen(crypt_path, “wb”);

//一次读取一个字符

int ch;

int i = 0;//循环使用密码中的字母进行疑惑运算

int pwd_len = strlen(password);//密码的长度

while ((ch = fgetc(normal_fp)) != EOF) { //End of File

//写入(异或运算)

fputc(ch ^ password[i % pwd_len], crypt_fp);

i++;

}

//关闭

fclose(crypt_fp);

fclose(normal_fp);

(*env)->ReleaseStringUTFChars(env, normal_path_jstr, normal_path);

(*env)->ReleaseStringUTFChars(env, crypt_path_jstr, crypt_path);

}

//解密

JNIEXPORT void JNICALL Java_cn_tsou_ndkfilecrypt_ndk_Cryptor_decrypt

(JNIEnv *env, jclass jcls, jstring crypt_path_jstr, jstring decrypt_path_jstr) {

//jstring ->char*

const char *crypt_path = (*env)->GetStringUTFChars(env, crypt_path_jstr, NULL);

const char *decrypt_path = (*env)->GetStringUTFChars(env, decrypt_path_jstr, NULL);

//打开文件

FILE *crypt_fp = fopen(crypt_path, “rb”);

FILE *decrypt_fp = fopen(decrypt_path, “wb”);

//一次读取一个字符

int ch;

int i = 0;//循环使用密码中的字母进行异或运算

int pwd_len = strlen(password);//密码的长度

while ((ch = fgetc(crypt_fp)) != EOF) { //End of File

//写入(异或运算)

fputc(ch ^ password[i % pwd_len], decrypt_fp);

i++;

}

//关闭

fclose(decrypt_fp);

fclose(crypt_fp);

(*env)->ReleaseStringUTFChars(env, crypt_path_jstr, crypt_path);

(*env)->ReleaseStringUTFChars(env, decrypt_path_jstr, decrypt_path);

}

//获取文件大小

int get_file_size(char *path) {

FILE *fp = fopen(path, “rb”);

fseek(fp, 0, SEEK_END);

return ftell(fp);

}

//拆分

JNIEXPORT void JNICALL Java_cn_tsou_ndkfilecrypt_ndk_Cryptor_diff

(JNIEnv *env, jclass jcls, jstring path_jstr, jstring path_pattern_jstr, jint file_num) {

//jstring ->char*

//需要分割的文件路径

const char *path = (*env)->GetStringUTFChars(env, path_jstr, NULL);

const char *path_pattern = (*env)->GetStringUTFChars(env, path_pattern_jstr, NULL);

//得到分割之后的子文件路径列表

char **pathches = malloc(sizeof(char *) * file_num);

int i = 0;

//不断读取path文件,循环写入file_num个文件中

for (; i < file_num; ++i) {

pathches[i] = malloc(sizeof(char) * 100);

//元素赋值

//需要分割的文件:C://jason/001.jpg

// 子文件:C://jason/001_%d.jpg

sprintf(pathches[i], path_pattern, (i + 1));

LOGI(“patch path:%s”, pathches[i]);

}

/**

  • 整除:

  • 文件大小:90,分成9个文件,每个文件10

  • 不整除:

  • 文件大小:110,分成9个文件,

  • 前(9-1)个文件为(110/(9-1))=13

  • 最后一个文件(110%(9-1))=6

  • (这个逻辑有问题104出现最后一个文件为0的问题,可自行判断)

*/

int filesize = get_file_size(path);

FILE *fpr = fopen(path, “rb”);

//整除

if (filesize % file_num == 0) {

//单个文件大小

int part = filesize / file_num;

i = 0;

//逐一写入不同的分割子文件中

for (; i < file_num; ++i) {

FILE *fpw = fopen(pathches[i], “wb”);

int j = 0;

for (; j < part; ++j) {

//边读编写

fputc(fgetc(fpr), fpw);

}

fclose(fpw);

}

} else {

//不能整除

int part = filesize / (file_num - 1);

i = 0;

//逐一写入不同的分割子文件中

for (; i < file_num - 1; ++i) {

FILE *fpw = fopen(pathches[i], “wb”);

int j = 0;

for (; j < part; ++j) {

//边读编写

fputc(fgetc(fpr), fpw);

}

fclose(fpw);

}

//最后一个

FILE *fpw = fopen(pathches[file_num - 1], “wb”);

i = 0;

for (; i < filesize % (file_num - 1); ++i) {

//边读编写

fputc(fgetc(fpr), fpw);

}

fclose(fpw);

}

fclose(fpr);

//释放

i = 0;

for (; i < file_num; ++i) {

free(pathches[i]);

}

free(pathches);

(*env)->ReleaseStringUTFChars(env, path_jstr, path);

(*env)->ReleaseStringUTFChars(env, path_pattern_jstr, path_pattern);

}

//合并

JNIEXPORT void JNICALL Java_cn_tsou_ndkfilecrypt_ndk_Cryptor_path

(JNIEnv *env, jclass jcls, jstring path_pattern_jstr, jint file_num, jstring total_jstr) {

//合并之后的文件

const char *merge_path = (*env)->GetStringUTFChars(env, total_jstr, NULL);

//分割指纹机的pattern

const char *path_pattern = (*env)->GetStringUTFChars(env, path_pattern_jstr, NULL);

//得到分割之后的子文件路径列表

char **pathches = malloc(sizeof(char *) * file_num);

int i = 0;

//不断读取path文件,循环写入file_num个文件中

for (; i < file_num; ++i) {

pathches[i] = malloc(sizeof(char) * 100);

//元素赋值

//需要分割的文件:C://jason/001.jpg

// 子文件:C://jason/001_%d.jpg

sprintf(pathches[i], path_pattern, (i + 1));

LOGI(“patch path:%s”, pathches[i]);

}

FILE *fpw = fopen(merge_path,“wb”);

//把所有的分割文件读取一遍,写入总的文件中

i=0;

for (; i < file_num; ++i) {

//每个子文件的大小

int filesize=get_file_size(pathches[i]);

FILE *fpr=fopen(pathches[i],“rb”);

int j=0;

for (; j <filesize ; ++j) {

fputc(fgetc(fpr),fpw);

}

fclose(fpr);

}

fclose(fpw);

//释放

i = 0;

for (; i < file_num; ++i) {

free(pathches[i]);

}

free(pathches);

(*env)->ReleaseStringUTFChars(env, total_jstr, merge_path);

(*env)->ReleaseStringUTFChars(env, path_pattern_jstr, path_pattern);

}

  • 调用

i
mport android.Manifest;

import android.content.pm.PackageManager;

import android.os.Bundle;

import android.os.Environment;

import android.support.annotation.NonNull;

import android.support.v4.app.ActivityCompat;

import android.support.v7.app.AppCompatActivity;

import android.util.Log;

import android.view.View;

import java.io.File;

import cn.tsou.ndkfilecrypt.ndk.Cryptor;

public class MainActivity extends AppCompatActivity {

String[] mPermissionList = new String[]{

Manifest.permission.WRITE_EXTERNAL_STORAGE,

Manifest.permission.READ_EXTERNAL_STORAGE};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

@Override

public void onAttachedToWindow() {

super.onAttachedToWindow();

ActivityCompat.requestPermissions(MainActivity.this, mPermissionList, 100);

}

@Override

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

switch (requestCode) {

case 100:

boolean writeExternalStorage = grantResults[0] == PackageManager.PERMISSION_GRANTED;

boolean readExternalStorage = grantResults[1] == PackageManager.PERMISSION_GRANTED;

if (grantResults.length > 0 && writeExternalStorage && readExternalStorage) {

}

break;

}

}

/**

  • 加密

  • @param view

*/

public void mCrypt(View view) {

String normal_path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar +

“huangxiaoguo” + File.separatorChar + “001.png”;

String crypt_path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar +

lts[1] == PackageManager.PERMISSION_GRANTED;

if (grantResults.length > 0 && writeExternalStorage && readExternalStorage) {

}

break;

}

}

/**

  • 加密

  • @param view

*/

public void mCrypt(View view) {

String normal_path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar +

“huangxiaoguo” + File.separatorChar + “001.png”;

String crypt_path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar +

标签:NDK,file,crypt,ndk,jstr,char,开发,env,path
来源: https://blog.csdn.net/m0_66264630/article/details/122733710

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有