//
// Created by #Suyghur, on 2021/10/15.
//


#include "yylog.h"
#include "aes_kit.h"


jstring AesKit::Encrypt(JNIEnv *env, const char *input) {
    jclass clz = env->FindClass("cn/yyxx/support/encryption/aes/AesUtils");
    if (clz == nullptr) {
        LOGD("aes impl clz is nullptr !!!");
        return env->NewStringUTF("");
    }
    const char *method_name = "encrypt2hex";
    const char *sig = "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;";
    jmethodID mid = env->GetStaticMethodID(clz, method_name, sig);
    jstring key = env->NewStringUTF(AES_KEY);
    jstring iv = env->NewStringUTF(IV);
    jstring data = env->NewStringUTF(input);
    auto result = (jstring) env->CallStaticObjectMethod(clz, mid, key, iv, data);
    env->DeleteLocalRef(clz);
    env->DeleteLocalRef(key);
    env->DeleteLocalRef(iv);
    env->DeleteLocalRef(data);
    return result;
}

jstring AesKit::Decrypt(JNIEnv *env, const char *input) {
    jclass clz = env->FindClass("cn/yyxx/support/encryption/aes/AesUtils");
    if (clz == nullptr) {
        LOGD("aes impl clz is nullptr !!!");
        return env->NewStringUTF("");
    }
    const char *method_name = "decrypt2hex";
    const char *sig = "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;";
    jmethodID mid = env->GetStaticMethodID(clz, method_name, sig);
    jstring key = env->NewStringUTF(AES_KEY);
    jstring iv = env->NewStringUTF(IV);
    jstring data = env->NewStringUTF(input);
    auto result = (jstring) env->CallStaticObjectMethod(clz, mid, key, iv, data);
    env->DeleteLocalRef(clz);
    env->DeleteLocalRef(key);
    env->DeleteLocalRef(iv);
    env->DeleteLocalRef(data);
    return result;
}


