jstring charToJString(JNIEnv *env, char *pat) {
jclass strClass = env->FindClass("java/lang/String");
jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
jbyteArray bytes = env->NewByteArray(strlen(pat));
env->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte *) pat);
jstring encoding = env->NewStringUTF("utf-8");
return (jstring) env->NewObject(strClass, ctorID, bytes, encoding);
}
jbyteArray charToJByteArray(JNIEnv *env, unsigned char *buf, int len) {
// size_t len = strlen(reinterpret_cast<const char *>(buf));
jbyteArray array = env->NewByteArray(len);
env->SetByteArrayRegion(array, 0, len, reinterpret_cast<jbyte *>(buf));
return array;
}
char *jByteArrayToChar(JNIEnv *env, jbyteArray buf) {
char *chars = NULL;
jbyte *bytes;
bytes = env->GetByteArrayElements(buf, 0);
int chars_len = env->GetArrayLength(buf);
chars = new char[chars_len + 1];
memset(chars, 0, chars_len + 1);
memcpy(chars, bytes, chars_len);
chars[chars_len] = 0;
env->ReleaseByteArrayElements(buf, bytes, 0);
return chars;
}
extern "C"
JNIEXPORT jobject JNICALL
Java_com_qtec_keyservice_KeyServiceJni_getQKey(JNIEnv *env, jclass clazz, jlong ul_handle,
jint ui_key_len, jstring uc_key_id, jstring uc_key) {
ULONG ulHandle = static_cast<ULONG>(ul_handle);
UINT uiKeyLen = static_cast<UINT>(ui_key_len);
unsigned char *cUcKeyId = (unsigned char *) env->GetStringUTFChars(uc_key_id, 0);
unsigned char *cUcKey = (unsigned char *) env->GetStringUTFChars(uc_key, 0);
LOGI("INFO———— getQKey start +++++++++++++++++++++++++++++++++++++++++++++++++++++++");
LOGI("INFO———— ulHandle = %lu", ulHandle);
LOGI("INFO———— uiKeyLen = %d", uiKeyLen);
LOGI("INFO———— cUcKeyId = %s", cUcKeyId);
LOGI("INFO———— cUcKey = %s", cUcKey);
jint result = QTEC_SAF_GetQKey(ulHandle, uiKeyLen, cUcKeyId, cUcKey);
for (int i = 0; i < 32; ++i) {
LOGI("keyId %x", cUcKeyId[i]);
}
for (int i = 0; i < 32; ++i) {
LOGI("key %x", cUcKey[i]);
}
jbyteArray jKeyId = charToJByteArray(env, cUcKeyId, 16);
jbyteArray jKey = charToJByteArray(env, cUcKey, uiKeyLen);
jclass resultClass = env->FindClass("com/qtec/keyservice/KeyResult");
jmethodID constructMethod = env->GetMethodID(resultClass, "<init>", "(I[B[B)V");
jobject keyResult = env->NewObject(resultClass, constructMethod, result, jKeyId, jKey);
LOGI("INFO———— getQKey end +++++++++++++++++++++++++++++++++++++++++++++++++++++++");
return keyResult;
}