123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- //
- // Created by #Suyghur, on 2021/06/09.
- //
- #include <jni.h>
- #include <toolkit.h>
- #include <rsa/rsa_utils.h>
- #include <comm_map.h>
- #include <params_kit.h>
- #include <request_kit.h>
- #include <md5/md5.h>
- #include "include/logger.h"
- #include "aes/aes_utils.h"
- static void GetAesKey(JNIEnv *env, jobject thiz) {
- uint8_t *aes_key = AesUtils::GetKey();
- uint8_t *iv = AesUtils::GetIv(aes_key);
- LOGD("%s", aes_key);
- LOGD("%s", iv);
- LOGD("------ invoke free() ------ ");
- free(aes_key);
- free(iv);
- // free((void *) aes_key);
- // free((void *) iv);
- aes_key = nullptr;
- iv = nullptr;
- LOGD("%s", aes_key);
- LOGD("%s", iv);
- }
- //static jstring AesEncrypt(JNIEnv *env, jobject thiz, jstring src) {
- // const char *src_ = env->GetStringUTFChars(src, JNI_FALSE);
- // char *raw = AesUtils::Encrypt(src_);
- // LOGD("%s", raw);
- //
- // env->ReleaseStringUTFChars(src, src_);
- //
- // jstring result = ToolKit::GetJString(env, raw);
- // free(raw);
- //
- // return result;
- //}
- static jstring AesDecrypt(JNIEnv *env, jobject thiz, jstring raw, jstring key) {
- const char *raw_ = env->GetStringUTFChars(raw, JNI_FALSE);
- const auto *key_ = reinterpret_cast<const uint8_t *>(env->GetStringUTFChars(key, JNI_FALSE));
- char *src = AesUtils::Decrypt(raw_, key_);
- env->ReleaseStringUTFChars(raw, raw_);
- jstring result = ToolKit::GetJString(env, src);
- free(src);
- return result;
- }
- static jstring RsaEncrypt(JNIEnv *env, jobject thiz, jstring src) {
- const char *src_ = env->GetStringUTFChars(src, JNI_FALSE);
- std::string key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCAVatPhPaZtxBkLBZDnOLf7no6VfnjBRMXhpOnWmkkDX4zurB/OKisYDFnR0UUVnKn717+absfLlDk9HZWBcTMznsUju9fuXlu3Elr8HeI3en7E0KIQzxkY1GT1+qtZ+tCjWuyMmUw4vWpO8/MRU3f6nc4io3w+7N+dP24BIo0ZwIDAQAB";
- std::string raw = RsaUtils::EncryptByPublicKey(key, src_);
- // LOGD("base64 raw : %s", raw.c_str());
- env->ReleaseStringUTFChars(src, src_);
- jstring result = ToolKit::GetJString(env, raw.c_str());
- return result;
- }
- static jstring RsaDecrypt(JNIEnv *env, jobject thiz, jstring raw) {
- const char *raw_ = env->GetStringUTFChars(raw, JNI_FALSE);
- // std::string key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCAVatPhPaZtxBkLBZDnOLf7no6VfnjBRMXhpOnWmkkDX4zurB/OKisYDFnR0UUVnKn717+absfLlDk9HZWBcTMznsUju9fuXlu3Elr8HeI3en7E0KIQzxkY1GT1+qtZ+tCjWuyMmUw4vWpO8/MRU3f6nc4io3w+7N+dP24BIo0ZwIDAQAB";
- std::string key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCbROlODv7iVaFhRAomQ9kSoV+whoebwVUDWUd968V8jbLQg0bAh6ad22O1bo33f4zNmIuDzdQMGOVgRyuvKsS9GyE+gh4XPDpSUmi/bZTl0wr8Y/yTi8eW+sXxn6Ao4oo4bg+qPewUxjTS2u4Pe1Zl3IC48HFALTAo0PSSXS5b7wIDAQAB";
- std::string src = RsaUtils::DecryptByPublicKey2(env, key, raw_);
- env->ReleaseStringUTFChars(raw, raw_);
- jstring result = ToolKit::GetJString(env, src.c_str());
- return result;
- }
- static void InitSdkDrive(JNIEnv *env, jobject thiz, jobject context) {
- CommMap::GetInstance()->Init(env, context);
- Logger::is_debug = ParamsKit::GetDebugModel(env, context);
- Logger::D(env, ToolKit::ToJsonString(CommMap::GetInstance()->comm_params));
- }
- static void SetParam(JNIEnv *env, jobject thiz, jstring key, jstring value) {
- std::string key_ = ToolKit::JString2String(env, key);
- std::string value_ = ToolKit::JString2String(env, value);
- if (key_ == "device_id") {
- if (value_ == CommMap::GetInstance()->comm_params["android_id"].asString()) {
- CommMap::GetInstance()->comm_params["id_type"] = 1;
- } else {
- CommMap::GetInstance()->comm_params["id_type"] = 0;
- }
- }
- CommMap::GetInstance()->comm_params[key_] = value_;
- Logger::D(env, ToolKit::ToJsonString(CommMap::GetInstance()->comm_params));
- }
- static jstring GetParam(JNIEnv *env, jobject thiz, jstring key) {
- std::string key_ = ToolKit::JString2String(env, key);
- return env->NewStringUTF(CommMap::GetInstance()->comm_params[key_].asString().c_str());
- }
- static jstring InvokeJob(JNIEnv *env, jobject thiz, jobject context, jstring data) {
- std::string enc = RequestKit::EncryptRequest2(env, context, data);
- // env->ReleaseStringUTFChars(raw, raw_);
- jstring result = ToolKit::GetJString(env, enc.c_str());
- // free(src);
- return result;
- }
- static jstring ParseJob(JNIEnv *env, jobject thiz, jobject context, jstring data) {
- std::string raw = RequestKit::DecryptResponse2(env, context, data);
- jstring result = ToolKit::GetJString(env, raw.c_str());
- return result;
- // time_t ts = time(nullptr);
- // LOGD("%ld", ts);
- // char *key = AesUtils::GetRawKey();
- // return env->NewStringUTF("");
- }
- static JNINativeMethod gMethod[] = {
- {"initSdkDrive", "(Landroid/content/Context;)V", (void *) InitSdkDrive},
- {"setParam", "(Ljava/lang/String;Ljava/lang/String;)V", (void *) SetParam},
- {"getParam", "(Ljava/lang/String;)Ljava/lang/String;", (void *) GetParam},
- {"invokeJob", "(Landroid/content/Context;Ljava/lang/String;)Ljava/lang/String;", (void *) InvokeJob},
- {"parseJob", "(Landroid/content/Context;Ljava/lang/String;)Ljava/lang/String;", (void *) ParseJob},
- // {"getAesKey", "()V", (void *) GetAesKey},
- // {"aesEncrypt", "(Ljava/lang/String;)Ljava/lang/String;", (void *) AesEncrypt},
- // {"aesDecrypt", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", (void *) AesDecrypt},
- // {"rsaEncrypt", "(Ljava/lang/String;)Ljava/lang/String;", (void *) RsaEncrypt},
- // {"rsaDecrypt", "(Ljava/lang/String;)Ljava/lang/String;", (void *) RsaDecrypt}
- };
- extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
- JNIEnv *env = nullptr;
- if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
- return JNI_ERR;
- }
- jclass clz = env->FindClass("cn/yyxx/columbus/core/impl/SdkDrive");
- if (env->RegisterNatives(clz, gMethod, sizeof(gMethod) / sizeof(gMethod[0])) < 0) {
- return JNI_ERR;
- }
- return JNI_VERSION_1_6;
- }
|