直接贴代码,JNI开发步骤:
1.写native方法
/**
/**
* 解密单个文件
* @param src_filename
* @param des_filename
* @param password
* @return
*/
public native int decryptFile(String src_filename,String des_filename, String password);
2.生成.h头文件
cd /Users/duqian/AndroidMac/workspace/OairEncryptFiles/bin/classes
javah -jni com.duqian.jni.OairDataProvider
3.c实现逻辑部分
实现以上生成的.h文件,部分代码
/*
* Class: com_duqian_jni_OairDataProvider
* Method: decryptFile
* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_duqian_jni_OairDataProvider_decryptFile(
JNIEnv * env,jobject obj,jstring src_path,jstring des_path,jstring pwd) {
char* src_filename = Jstring2CStr(env, src_path);
char* des_filename = Jstring2CStr(env, des_path);
char* password = Jstring2CStr(env, pwd);
return q_decrypt_file(src_filename, des_filename, password);
}
4.交叉编译,生成.so文件。
---Android.mk文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := duqian_encryption
LOCAL_SRC_FILES := duqian_encryption.c
LOCAL_LDLIBS :=-llog
include $(BUILD_SHARED_LIBRARY)
编译命令:
ndk-build
5.调用本地方法
private static String libName = "duqian_encryption";
static {
System.loadLibrary(libName);
}
public void testJni(){
int result = decryptFile("c:\\a.jpg","d:\\b.jpg", "password");
//加密结果
}
在android代码中使用native方法进行文件加解密,调用的是c代码,比较安全。