Android NDK基础15:文件加解密_文件合并

文件加解密

1
2
3
4
5
6
7
8
9
10
11
public class Cryptor {

public native static void crypt(String normal_path, String crypt_path);

public native static void decrypt(String crypt_path, String decrypt_path);

static {
System.loadLibrary("FileCrypt");
}

}

FileCrypt.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "cn_appblog_ndkfilecrypt_Cryptor.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char password[] = "appblog.cn";

//加密
JNIEXPORT void JNICALL Java_cn_appblog_ndkfilecrypt_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, JNI_FALSE);
const char* crypt_path = (*env)->GetStringUTFChars(env, crypt_path_jstr, JNI_FALSE);

//打开文件
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);
}

//解密
JNIEXPORT void JNICALL Java_cn_appblog_ndkfilecrypt_Cryptor_decrypt(
JNIEnv * env, jclass jcls, jstring crypt_path_jstr, jstring decrypt_path_jstr) {

const char* crypt_path = (*env)->GetStringUTFChars(env, crypt_path_jstr, JNI_FALSE);
const char* decrypt_path = (*env)->GetStringUTFChars(env, decrypt_path_jstr, JNI_FALSE);

//打开文件
FILE *normal_fp = fopen(crypt_path, "rb");
FILE *crypt_fp = fopen(decrypt_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);
}

Application.mk

1
2
3
4
5
6
7
8
9
10
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := FileCrypt
LOCAL_SRC_FILES := FileCrypt.c

LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

Application.mk

1
2
APP_ABI := armeabi armeabi-v7a arm64-v8a x86 x86_64 mips mips64
APP_PLATFORM := android-9

文件合并

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class FilePatch {

/**
* 拆分
* @param origin_path
* @param path_pattern
* @param count
*/
public native static void diff(String origin_path, String path_pattern, int count);

/**
* 合并
* @param merge_path
* @param path_pattern
* @param count
*/
public native static void patch(String merge_path, String path_pattern, int count);

static{
System.loadLibrary("FilePatch");
}

}

FilePatch.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "cn_appblog_ndkfilepatch_FilePatch.h"

#include <stdlib.h>
#include <stdio.h>

#include <android/log.h>

#ifndef LOG
#define LOG_TAG "yezhou"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#endif

//获取文件大小
long get_file_size(const char *path) {
FILE *fp = fopen(path, "rb");
fseek(fp, 0, SEEK_END);
return ftell(fp);
}

//拆分
JNIEXPORT void JNICALL Java_cn_appblog_ndkfilepatch_FilePatch_diff
(JNIEnv *env, jclass jcls, jstring origin_path_jstr, jstring path_pattern_jstr, jint file_count) {
//jstring -> char*
//需要分割的文件路径
const char* origin_path = (*env)->GetStringUTFChars(env, origin_path_jstr, NULL);
const char* path_pattern = (*env)->GetStringUTFChars(env, path_pattern_jstr, NULL);

//得到分割之后的子文件的路径列表
char **patches = malloc(sizeof(char*) * file_count);
int i = 0;
for (; i < file_count; i++) {
patches[i] = malloc(sizeof(char) * 100);
//元素赋值
//需要分割的文件:C://jason/liuyan.png
//子文件:C://jason/liuyan_%d.png
sprintf(patches[i], path_pattern, (i+1));
LOGI("patch path:%s", patches[i]);
}

//不断读取path文件,循环写入file_count个文件中
//整除
// 文件大小:90,分成9个文件,每个文件10
//不整除
// 文件大小:110,分成9个文件,
// 前(9-1)个文件为(110/(9-1))=13
// 最后一个文件(110%(9-1))=6
int filesize = get_file_size(origin_path);
FILE *fpr = fopen(origin_path, "rb");
//整除
if(filesize % file_count == 0) {
//单个文件大小
int part = filesize / file_count;
i = 0;
//逐一写入不同的分割子文件中
for (; i < file_count; i++) {
FILE *fpw = fopen(patches[i], "wb");
int j = 0;
for(; j < part; j++) {
//边读边写
fputc(fgetc(fpr), fpw);
}
fclose(fpw);
}
} else {
//不整除
int part = filesize / (file_count-1);
i = 0;
//逐一写入不同的分割子文件中
for (; i < file_count-1; i++) {
FILE *fpw = fopen(patches[i], "wb");
int j = 0;
for(; j < part; j++) {
//边读边写
fputc(fgetc(fpr), fpw);
}
fclose(fpw);
}
//最后一个文件
FILE *fpw = fopen(patches[file_count-1], "wb");
i = 0;
for(; i < filesize % (file_count-1); i++) {
fputc(fgetc(fpr), fpw);
}
fclose(fpw);
}

//关闭被分割的文件
fclose(fpr);

//释放
i = 0;
for(; i < file_count; i++) {
free(patches[i]);
}
free(patches);

(*env)->ReleaseStringUTFChars(env, origin_path_jstr, origin_path);
(*env)->ReleaseStringUTFChars(env, path_pattern_jstr, path_pattern);
}

//合并
JNIEXPORT void JNICALL Java_cn_appblog_ndkfilepatch_FilePatch_patch
(JNIEnv *env, jclass jcls, jstring merge_path_jstr, jstring path_pattern_jstr, jint file_count) {
//合并之后的文件
const char* merge_path = (*env)->GetStringUTFChars(env, merge_path_jstr, NULL);
//分割子文件的pattern
const char* path_pattern = (*env)->GetStringUTFChars(env, path_pattern_jstr, NULL);

//得到分割之后的子文件的路径列表
char **patches = malloc(sizeof(char*) * file_count);
int i = 0;
for (; i < file_count; i++) {
patches[i] = malloc(sizeof(char) * 100);
//元素赋值
//需要分割的文件:C://jason/liuyan.png
//子文件:C://jason/liuyan_%d.png
sprintf(patches[i], path_pattern, (i+1));
LOGI("patch path:%s", patches[i]);
}

FILE *fpw = fopen(merge_path,"wb");
//把所有的分割文件读取一遍,写入一个总的文件中
i = 0;
for(; i < file_count; i++) {
//每个子文件的大小
int filesize = get_file_size(patches[i]);
FILE *fpr = fopen(patches[i], "rb");
int j = 0;
for (; j < filesize; j++) {
fputc(fgetc(fpr),fpw);
}
fclose(fpr);
}
fclose(fpw);

//释放
i = 0;
for(; i < file_count; i++) {
free(patches[i]);
}
free(patches);

(*env)->ReleaseStringUTFChars(env, merge_path_jstr, merge_path);
(*env)->ReleaseStringUTFChars(env, path_pattern_jstr, path_pattern);
}

Application.mk

1
2
3
4
5
6
7
8
9
10
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := FilePatch
LOCAL_SRC_FILES := FilePatch.c

LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

Application.mk

1
2
APP_ABI := armeabi armeabi-v7a arm64-v8a x86 x86_64 mips mips64
APP_PLATFORM := android-9

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :