Answer the question
In order to leave comments, you need to log in
Why is the text not encrypted?
I'm trying to insert an encryption module written in C / C ++ into an android application. It seems to be inserted, everything seems to work, but the application does not work.
native-lib.cpp
#include <jni.h>
#include <string>
#include "gost34-12-15.h"
#include "gost34-13-15.h"
#include "main.cpp"
#include <android/log.h>
std::string jstring2string (JNIEnv * env, jstring jStr);
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_maxim_onlyforc_MainActivity_stringFromJNI(
JNIEnv *env,
jobject obj, jstring msg) {
jstring msg2= crypto(env, msg);
std::string text = jstring2string(env, msg2) + " проверка";
return env->NewStringUTF(text.c_str());
}
std::string jstring2string (JNIEnv * env, jstring jStr) {
if (!jStr)
return "";
const jclass stringClass = env->GetObjectClass(jStr);
const jmethodID getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
const jbyteArray stringJbytes = (jbyteArray) env->CallObjectMethod(jStr, getBytes, env->NewStringUTF("UTF-8"));
size_t length = (size_t) env->GetArrayLength(stringJbytes);
jbyte* pBytes = env->GetByteArrayElements(stringJbytes, NULL);
std::string ret = std::string((char *)pBytes, length);
env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT);
env->DeleteLocalRef(stringJbytes);
env->DeleteLocalRef(stringClass);
return ret;
}
#include <jni.h>
#include <string>
#include "gost34-12-15.h"
#include "gost34-13-15.h"
unsigned char* jstring2char(JNIEnv * env, jstring);
jbyteArray StringToJByteArray(JNIEnv * env, const std::string &nativeString);
jstring crypto(JNIEnv *env, jstring msg)
{
// Открытый текст для ГОСТ 34.13-15 \\
byte* text = jstring2char(env, msg);
byte key[] = { // ключ
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11,
0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xfe, 0xdc, 0xba, 0x98, 0x76,
0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
byte IV[] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xce, 0xf0, 0xa1, 0xb2, //синхр. посылка
0xc3, 0xd4, 0xe5, 0xf0, 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67,
0x78, 0x89, 0x90, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19};
init128Gost34_12_15(key);
encrypt_decrypt_OFB(IV, text, sizeof (text));
close128Gost34_12_15();
closeGost34_13_15(IV);
jstring newText = (*env).NewStringUTF((const char*) text);
byte* text2 = jstring2char(env, newText);
return newText;
}
unsigned char* jstring2char (JNIEnv * env, jstring jStr) {
const jclass stringClass = env->GetObjectClass(jStr);
const jmethodID getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
const jbyteArray stringJbytes = (jbyteArray) env->CallObjectMethod(jStr, getBytes, env->NewStringUTF("UTF-8"));
int len = env->GetArrayLength(stringJbytes);
unsigned char* buf = new unsigned char[len];
env->GetByteArrayRegion(stringJbytes, 0, len, reinterpret_cast<jbyte*>(buf));
return buf;
}
jbyteArray StringToJByteArray(JNIEnv * env, const std::string &nativeString) {
jbyteArray arr = env->NewByteArray(nativeString.length());
env->SetByteArrayRegion(arr,0,nativeString.length(),(jbyte*)nativeString.c_str());
return arr;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question