前言
我會(huì)在小專欄和簡(jiǎn)書(shū)一起分享。
最近在忙其他事情芹关,今天想起JNI做加解密不錯(cuò)撒会,于是我調(diào)查了一下AES使用JNI實(shí)現(xiàn),試驗(yàn)過(guò)程發(fā)現(xiàn)用C或者C++出了很多問(wèn)題蔬啡,于是發(fā)現(xiàn)有人使用一種巧妙的方式诲侮,用Java做加解密,只是把加解密的Key放在SO庫(kù)中箱蟆,這里我要講的就是這種方式沟绪。
正文
新建java類
public class AESjni {
public static native String myEncrypt(String str);
public static native String myDecrypt(String str);
}
還是使用我們的Tools工具生成.h
文件
接著編寫(xiě)c文件
#include "com_crayfish_ndkproject_AESjni.h"
#include <stdlib.h>
const char *DES_KEY = "12345678912345678912345678912345";
/*
* Class: com_jzp_myapplication_JniUtils
* Method: getString
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_crayfish_ndkproject_AESjni_myEncrypt
(JNIEnv *env, jclass jclass1, jstring jstr)
{
if (jstr == NULL) {
return NULL;
}
jstring key;
jstring result;
jclass AESencrypt;
jmethodID mid;
AESencrypt = (*env)->FindClass(env, "com/crayfish/ndkproject/AESjni");
if (NULL == AESencrypt) {
return NULL;
}
mid = (*env)->GetStaticMethodID(env, AESencrypt, "encrypt",
"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
if (NULL == mid) {
(*env)->DeleteLocalRef(env, AESencrypt);
return NULL;
}
key = (*env)->NewStringUTF(env, DES_KEY);
result = (*env)->CallStaticObjectMethod(env, AESencrypt, mid, key, jstr);
(*env)->DeleteLocalRef(env, AESencrypt);
(*env)->DeleteLocalRef(env, key);
return result;
}
JNIEXPORT jstring JNICALL Java_com_crayfish_ndkproject_AESjni_myDecrypt
(JNIEnv *env, jclass jclass1, jstring jstr)
{
if (jstr == NULL) {
return NULL;
}
jstring key;
jstring result;
jclass AESencrypt;
jmethodID mid;
AESencrypt = (*env)->FindClass(env, "com/crayfish/ndkproject/AESjni");
if (NULL == AESencrypt) {
return NULL;
}
mid = (*env)->GetStaticMethodID(env, AESencrypt, "decrypt",
"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
if (NULL == mid) {
(*env)->DeleteLocalRef(env, AESencrypt);
return NULL;
}
key = (*env)->NewStringUTF(env, DES_KEY);
result = (*env)->CallStaticObjectMethod(env, AESencrypt, mid, key, jstr);
(*env)->DeleteLocalRef(env, AESencrypt);
(*env)->DeleteLocalRef(env, key);
return result;
}
看到c
代碼中有讀取java類中encrypt
和decrypt
兩個(gè)方法了吧,我們把它們補(bǔ)上空猜,這里就是真正的加解密邏輯绽慈。
public static String encrypt(String key,String palinText) throws Exception{
//加密
return "";
}
public static String decrypt(String key,String palinText) throws Exception{
//解密
return "";
}
記得配置Android.mk
和build.gradle
include $(CLEAR_VARS)
LOCAL_MODULE := AESjni
LOCAL_SRC_FILES := aesjni.c
include $(BUILD_SHARED_LIBRARY)
ndk{
moduleName "AESjni" //生成的so名字
//abiFilters "armeabi", "armeabi-v7a", "x86" //輸出指定三種abi體系結(jié)構(gòu)下的so庫(kù)。目前可有可無(wú)抄肖。
}
結(jié)尾
今天的內(nèi)容很簡(jiǎn)單,到這就結(jié)束了窖杀。