android Jni练习-图片解密

     最近项目要做图片的加解密工作,从老板给的代码来看像是AES,但也有区别,所以也没有采用java那个封装好的包。给我的是C代码,之前android留了接口,所以还是采用Jni方式直接调吧。

1、先做了一个本地解密的demo,直接把图片用加密工具,加密。然后存到手机SD卡上。

2、准备工作,因为是在windows环境下做的,所以使用的是cygwin,NDK版本是R8,比较老了。配置方法网上搜一下就有,主要配置NDK路径。工程文件如下

        

3、把老板给的代码AES.C AES.h 复制到工程

4、编写mk文件

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
LOCAL_MODULE    := AES
LOCAL_SRC_FILES := AESAndroid.c  AES.c

include $(BUILD_SHARED_LIBRARY)

5.编写接口函数 即AESandroid.c

#include <string.h>
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include "memory.h"
#include <android/log.h>


#define LOG_TAG   "AESAndroid.c"
#define  LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)


#ifdef __cplusplus
extern "C" {
#endif

/*********************解密*********************/
JNIEXPORT jint JNICALL Java_androidJni_MyAESAlgorithm_fileDecrypt(JNIEnv *env,
		jclass thiz, jstring srcPath, jstring key, jstring resultPath,jint encrypt_type) {
	const char * sPath = (*env)->GetStringUTFChars(env,srcPath, NULL);
	const char * sKey = (*env)->GetStringUTFChars(env,key, NULL);
	const char * rPath = (*env)->GetStringUTFChars(env,resultPath, NULL);
	LOGI(sPath);
	LOGI(sKey);

	int len_skey = strlen(sKey);

	FILE* file = NULL;
	file = fopen(sPath, "rb");
	if (file == NULL) {
		LOGI("file is not ");
		return 131111;
	}
	fclose(file);
	file = NULL;
	unsigned char bKey[17] = { '\0' };
	unsigned char bIV[17] = { '\0' };
	memcpy(bKey, sKey, 16);
	memcpy(bIV, sKey + 16, 16);
	int flag = fileDecrypt(sPath, bKey, bIV, encrypt_type);


	(*env)->ReleaseStringUTFChars(env,resultPath, rPath);
	(*env)->ReleaseStringUTFChars(env,key, sKey);
	(*env)->ReleaseStringUTFChars(env,srcPath, sPath);
	return flag;
}


#ifdef __cplusplus
}
#endif

需要说明的是,文件头是可以自动生成的,函数名规则就是java_包名_类名_函数名。在Jni调用中C和C++还是有区别的 避免不必要的错误 最好不要混用,如过编译的是.cpp

(*env)要写成env,如果引的头文件是stido.h, 就别char *content=new char[iDataLen]了,用char *content = (char*)malloc(sizeof(char)*iDataLen);等等吧,我这C还是C89标准

在for循环里初始化变量都报错。

6.生成.so库

           简单说一下编译方法吧。启动cgywin

进入Jni文件夹,我的工程在E盘编程练习文件夹里

然后输入$NDK/ndk-build 就可以编译.so库了


7.java层写个类封装一下

public class MyAESAlgorithm
{
	

	public synchronized static native int fileDecrypt(String srcPath, String key, String resultPath,int encrypt_type);

	static
	{
		System.loadLibrary("AES");
	}
}
8.做好参数,传入即可
<pre name="code" class="java">private void testAES() {
		
		String Key = "0123456789abcdef0123456789abcdef";
		String srcPath = "/mnt/sdcard/UCDownloads/d.jpg";
		
		String resultPath="flie:///mnt/sdcard/UCDownloads/e.jpg";

		System.out.println("------------密钥----------------");
		System.out.print(Key);

		MyAESAlgorithm.fileDecrypt(srcPath, Key, resultPath,1);
		System.out.println();
		System.out.println("------------ END ----------------");
	}


 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值