sdk_drive.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // Created by #Suyghur, on 2021/06/09.
  3. //
  4. #include <jni.h>
  5. #include <toolkit.h>
  6. #include <rsa/rsa_utils.h>
  7. #include <comm_map.h>
  8. #include <params_kit.h>
  9. #include <request_kit.h>
  10. #include <md5/md5.h>
  11. #include "include/logger.h"
  12. #include "aes/aes_utils.h"
  13. static void GetAesKey(JNIEnv *env, jobject thiz) {
  14. uint8_t *aes_key = AesUtils::GetKey();
  15. uint8_t *iv = AesUtils::GetIv(aes_key);
  16. LOGD("%s", aes_key);
  17. LOGD("%s", iv);
  18. LOGD("------ invoke free() ------ ");
  19. free(aes_key);
  20. free(iv);
  21. // free((void *) aes_key);
  22. // free((void *) iv);
  23. aes_key = nullptr;
  24. iv = nullptr;
  25. LOGD("%s", aes_key);
  26. LOGD("%s", iv);
  27. }
  28. //static jstring AesEncrypt(JNIEnv *env, jobject thiz, jstring src) {
  29. // const char *src_ = env->GetStringUTFChars(src, JNI_FALSE);
  30. // char *raw = AesUtils::Encrypt(src_);
  31. // LOGD("%s", raw);
  32. //
  33. // env->ReleaseStringUTFChars(src, src_);
  34. //
  35. // jstring result = ToolKit::GetJString(env, raw);
  36. // free(raw);
  37. //
  38. // return result;
  39. //}
  40. static jstring AesDecrypt(JNIEnv *env, jobject thiz, jstring raw, jstring key) {
  41. const char *raw_ = env->GetStringUTFChars(raw, JNI_FALSE);
  42. const auto *key_ = reinterpret_cast<const uint8_t *>(env->GetStringUTFChars(key, JNI_FALSE));
  43. char *src = AesUtils::Decrypt(raw_, key_);
  44. env->ReleaseStringUTFChars(raw, raw_);
  45. jstring result = ToolKit::GetJString(env, src);
  46. free(src);
  47. return result;
  48. }
  49. static jstring RsaEncrypt(JNIEnv *env, jobject thiz, jstring src) {
  50. const char *src_ = env->GetStringUTFChars(src, JNI_FALSE);
  51. std::string key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCAVatPhPaZtxBkLBZDnOLf7no6VfnjBRMXhpOnWmkkDX4zurB/OKisYDFnR0UUVnKn717+absfLlDk9HZWBcTMznsUju9fuXlu3Elr8HeI3en7E0KIQzxkY1GT1+qtZ+tCjWuyMmUw4vWpO8/MRU3f6nc4io3w+7N+dP24BIo0ZwIDAQAB";
  52. std::string raw = RsaUtils::EncryptByPublicKey(key, src_);
  53. // LOGD("base64 raw : %s", raw.c_str());
  54. env->ReleaseStringUTFChars(src, src_);
  55. jstring result = ToolKit::GetJString(env, raw.c_str());
  56. return result;
  57. }
  58. static jstring RsaDecrypt(JNIEnv *env, jobject thiz, jstring raw) {
  59. const char *raw_ = env->GetStringUTFChars(raw, JNI_FALSE);
  60. // std::string key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCAVatPhPaZtxBkLBZDnOLf7no6VfnjBRMXhpOnWmkkDX4zurB/OKisYDFnR0UUVnKn717+absfLlDk9HZWBcTMznsUju9fuXlu3Elr8HeI3en7E0KIQzxkY1GT1+qtZ+tCjWuyMmUw4vWpO8/MRU3f6nc4io3w+7N+dP24BIo0ZwIDAQAB";
  61. std::string key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCbROlODv7iVaFhRAomQ9kSoV+whoebwVUDWUd968V8jbLQg0bAh6ad22O1bo33f4zNmIuDzdQMGOVgRyuvKsS9GyE+gh4XPDpSUmi/bZTl0wr8Y/yTi8eW+sXxn6Ao4oo4bg+qPewUxjTS2u4Pe1Zl3IC48HFALTAo0PSSXS5b7wIDAQAB";
  62. std::string src = RsaUtils::DecryptByPublicKey2(env, key, raw_);
  63. env->ReleaseStringUTFChars(raw, raw_);
  64. jstring result = ToolKit::GetJString(env, src.c_str());
  65. return result;
  66. }
  67. static void InitSdkDrive(JNIEnv *env, jobject thiz, jobject context) {
  68. CommMap::GetInstance()->Init(env, context);
  69. Logger::is_debug = ParamsKit::GetDebugModel(env, context);
  70. Logger::D(env, ToolKit::ToJsonString(CommMap::GetInstance()->comm_params));
  71. }
  72. static void SetParam(JNIEnv *env, jobject thiz, jstring key, jstring value) {
  73. std::string key_ = ToolKit::JString2String(env, key);
  74. std::string value_ = ToolKit::JString2String(env, value);
  75. if (key_ == "device_id") {
  76. if (value_ == CommMap::GetInstance()->comm_params["android_id"].asString()) {
  77. CommMap::GetInstance()->comm_params["id_type"] = 1;
  78. } else {
  79. CommMap::GetInstance()->comm_params["id_type"] = 0;
  80. }
  81. }
  82. CommMap::GetInstance()->comm_params[key_] = value_;
  83. Logger::D(env, ToolKit::ToJsonString(CommMap::GetInstance()->comm_params));
  84. }
  85. static jstring GetParam(JNIEnv *env, jobject thiz, jstring key) {
  86. std::string key_ = ToolKit::JString2String(env, key);
  87. return env->NewStringUTF(CommMap::GetInstance()->comm_params[key_].asString().c_str());
  88. }
  89. static jstring InvokeJob(JNIEnv *env, jobject thiz, jobject context, jstring data) {
  90. std::string enc = RequestKit::EncryptRequest2(env, context, data);
  91. // env->ReleaseStringUTFChars(raw, raw_);
  92. jstring result = ToolKit::GetJString(env, enc.c_str());
  93. // free(src);
  94. return result;
  95. }
  96. static jstring ParseJob(JNIEnv *env, jobject thiz, jobject context, jstring data) {
  97. std::string raw = RequestKit::DecryptResponse2(env, context, data);
  98. jstring result = ToolKit::GetJString(env, raw.c_str());
  99. return result;
  100. // time_t ts = time(nullptr);
  101. // LOGD("%ld", ts);
  102. // char *key = AesUtils::GetRawKey();
  103. // return env->NewStringUTF("");
  104. }
  105. static JNINativeMethod gMethod[] = {
  106. {"initSdkDrive", "(Landroid/content/Context;)V", (void *) InitSdkDrive},
  107. {"setParam", "(Ljava/lang/String;Ljava/lang/String;)V", (void *) SetParam},
  108. {"getParam", "(Ljava/lang/String;)Ljava/lang/String;", (void *) GetParam},
  109. {"invokeJob", "(Landroid/content/Context;Ljava/lang/String;)Ljava/lang/String;", (void *) InvokeJob},
  110. {"parseJob", "(Landroid/content/Context;Ljava/lang/String;)Ljava/lang/String;", (void *) ParseJob},
  111. // {"getAesKey", "()V", (void *) GetAesKey},
  112. // {"aesEncrypt", "(Ljava/lang/String;)Ljava/lang/String;", (void *) AesEncrypt},
  113. // {"aesDecrypt", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", (void *) AesDecrypt},
  114. // {"rsaEncrypt", "(Ljava/lang/String;)Ljava/lang/String;", (void *) RsaEncrypt},
  115. // {"rsaDecrypt", "(Ljava/lang/String;)Ljava/lang/String;", (void *) RsaDecrypt}
  116. };
  117. extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
  118. JNIEnv *env = nullptr;
  119. if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
  120. return JNI_ERR;
  121. }
  122. jclass clz = env->FindClass("cn/yyxx/columbus/core/impl/SdkDrive");
  123. if (env->RegisterNatives(clz, gMethod, sizeof(gMethod) / sizeof(gMethod[0])) < 0) {
  124. return JNI_ERR;
  125. }
  126. return JNI_VERSION_1_6;
  127. }