#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 +