Написал шифр. часть на C на Qt. Там все работало. Теперь хочу ее привинтить к Java в Android Studio, но появилась такая ошибка, хотя вроде все везде объявлено.
Ошибка №1. (файл native-lib.cpp)

MainAcrivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String msg = "Hello from Java";
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText(stringFromJNI(msg));
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI(String msg);
}
native-lib.cpp
#include <jni.h>
#include <string>
#include "gost34-12-15.h"
#include "gost34-13-15.h"
#include "main.cpp"
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= (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;
}
main.cpp
#include <jni.h>
#include <string>
#include "gost34-12-15.h"
#include "gost34-13-15.h"
unsigned char* jstring2char(JNIEnv * env, jstring);
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);
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;
}
gost34-13-15.h
#ifndef GOST341315_H
#define GOST341315_H
typedef unsigned char byte;
// --------------------------------------------------------------
/*
Назначение:
Шифрование или расшифрование входящего текста.
Входные параметры:
IV - синхр. посылка длиной m (256 бит - 32 байта)
text - входной открытый/шифр текст любой длины
size - длина текста
Возвращаемое значение: нет
*/
void encrypt_decrypt_OFB (byte* IV, byte* text, int size);
// --------------------------------------------------------------
/*
Назначение:
Очистка массива синх. посылки.
Входные параметры:
IV - синхр. посылка длиной m (256 бит - 32 байта)
Возвращаемое значение: нет
*/
void closeGost34_13_15(byte* IV);
// --------------------------------------------------------------
#endif // GOST341315_H
gost34-13-15.cpp
#include "gost34-12-15.h"
#include <cstring>
#include <cstdio>
#include <string.h>
using namespace std;
static int n = 16;;
//static int m = 32;
//static int s = 16;
byte text128[16];
byte resultMSB_Y[16];
byte resultLSB[16];
static void MSB(byte* massive, int n);
static void LSB(byte* massive, int n);
void encrypt_decrypt_OFB (byte* R, byte* text, int size) {
int q = size / n;
int ostatok = size % n;
for (int i = 0; i < q; i++) {
arraycopy(text, i * n, text128, 0, n);
MSB(R, n);
encrypt128Gost34_12_15(resultMSB_Y);
X(text128, resultMSB_Y);
LSB(R, n);
arraycopy(resultLSB, 0, R, 0, n);
arraycopy(resultMSB_Y, 0, R, n, n);
arraycopy(text128, 0, text, i * n, n);
}
if (ostatok != 0) {
byte textLess128[ostatok];
byte resultMSBshort [ostatok];
arraycopy(text, (q - 1) * n, textLess128, 0, ostatok);
MSB(R, n);
encrypt128Gost34_12_15(resultMSB_Y);
MSB(resultMSB_Y, ostatok);
arraycopy(resultMSB_Y, 0, resultMSBshort, 0, ostatok);
X(textLess128, resultMSBshort);
arraycopy(textLess128, 0, text, (q - 1) * n, ostatok);
}
}
static void MSB (byte* massive, int n) {
arraycopy(massive, 0, resultMSB_Y, 0, n);
}
static void LSB (byte* massive, int n) {
arraycopy(massive, n, resultLSB, 0, n);
}
void closeGost34_13_15(byte* R){
memset(R, 0, 32);
}
gost34-12-15.h
#ifndef GOST341215_H
#define GOST341215_H
typedef unsigned char byte;
//--------------------------------------------------------------
/*
Назначение:
Создать массив итерационных ключей
Входные параметры:
current_key - используемый ключ длиной 32 байта
text - текст длиной 16 байт
length_text - размер текста
Возвращаемое значение: нет
*/
void init128Gost34_12_15(byte* current_key);// , int length_current_key, byte* text, int length_text);
//--------------------------------------------------------------
/*
Назначение:
Копирование данных из массива masIn с позиции pos1 в массив masOut
с позиции pos2. Количество символов равно length
Входные параметры:
masIn - массив, откуда копируются данные; pos1 - в массиве masIn,
с которой копируются данные; masOut - массив, куда копируются и
записываются данные; pos2 - позиция в массиве masOut, с которой
ведется запись: length - количество перекопированных элементов
*/
void arraycopy(byte* masIn, int pos1, byte* masOut, int pos2, int length);
//--------------------------------------------------------------
/*
!!!Пользователи, преждем чем использовать данную функцию,
должны один раз вызвать функцию initK
Назначение:
Шифрует текст
Входные параметры:
msg - текст, который шифруется; msg_size - размер текста
Возвращаемое значение: нет
*/
void encrypt128Gost34_12_15(byte* msg);
//--------------------------------------------------------------
/*
Назначение:
Выполнение функции Х, она же - сложение двух аргументов по mod2
Входные параметры:
res_arg1 - первый массив, который в себя перезаписывает результат
операции; arg2 - второй массив, который складывается по mod2 с
первым
*/
void X(byte* res_and_arg1, const byte* arg2);
//--------------------------------------------------------------
/*
Назначение:
Копирование данных из массива masIn с позиции pos1 в массив masOut
с позиции pos2. Количество символов равно length
Входные параметры:
masIn - массив, откуда копируются данные; pos1 - в массиве masIn,
с которой копируются данные; masOut - массив, куда копируются и
записываются данные; pos2 - позиция в массиве masOut, с которой
ведется запись: length - количество перекопированных элементов
*/
//void arraycopy(byte* masIn, int pos1, byte* masOut, int pos2, int length);
//--------------------------------------------------------------
void close128Gost34_12_15();
#endif // GOST341215_H
gost34-12-15.cpp
#include "gost34-12-15.h"
#include <cstring>
#include <string.h>
using namespace std;
const byte pi[] = {
// 0 1 2 3 4 5 6 7
252, 238, 221, 17 , 207, 110, 49 , 22 ,//0
251, 196, 250, 218, 35 , 197, 4 , 77 ,//1
233, 119, 240, 219, 147, 46 , 153, 186,//2
23 , 54 , 241, 187, 20 , 205, 95 , 193,//3
249, 24 , 101, 90 , 226, 92 , 239, 33 ,//4
129, 28 , 60 , 66 , 139, 1 , 142, 79 ,//5
5 , 132, 2 , 174, 227, 106, 143, 160,//6
6 , 11 , 237, 152, 127, 212, 211, 31 ,//7
235, 52 , 44 , 81 , 234, 200, 72 , 171,//8
242, 42 , 104, 162, 253, 58 , 206, 204,//9
181, 112, 14 , 86 , 8 , 12 , 118, 18 ,//10
191, 114, 19 , 71 , 156, 183, 93 , 135,//11
21 , 161, 150, 41 , 16 , 123, 154, 199,//12
243, 145, 120, 111, 157, 158, 178, 177,//13
50 , 117, 25 , 61 , 255, 53 , 138, 126,//14
109, 84 , 198, 128, 195, 189, 13 , 87 ,//15
223, 245, 36 , 169, 62 , 168, 67 , 201,//16
215, 121, 214, 246, 124, 34 , 185, 3 ,//17
224, 15 , 236, 222, 122, 148, 176, 188,//18
220, 232, 40 , 80 , 78 , 51 , 10 , 74 ,//19
167, 151, 96 , 115, 30 , 0 , 98 , 68 ,//20
26 , 184, 56 , 130, 100, 159, 38 , 65 ,//21
173, 69 , 70 , 146, 39 , 94 , 85 , 47 ,//22
140, 163, 165, 125, 105, 213, 149, 59 ,//23
7 , 88 , 179, 64 , 134, 172, 29 , 247,//24
48 , 55 , 107, 228, 136, 217, 231, 137,//25
225, 27 , 131, 73 , 76 , 63 , 248, 254,//26
141, 83 , 170, 144, 202, 216, 133, 97 ,//27
32 , 113, 103, 164, 45 , 43 , 9 , 91 ,//28
203, 155, 37 , 208, 190, 229, 108, 82 ,//29
89 , 166, 116, 210, 230, 244, 180, 192,//30
209, 102, 175, 194, 57 , 75 , 99 , 182 };//31
const byte pi_reverse[] = {
165, 45 , 50 , 143, 14 , 48 , 56 , 192,
84 , 230, 158, 57 , 85 , 126, 82 , 145,
100, 3 , 87 , 90 , 28 , 96 , 7 , 24 ,
33 , 114, 168, 209, 41 , 198, 164, 63 ,
224, 39 , 141, 12 , 130, 234, 174, 180,
154, 99 , 73 , 229, 66 , 228, 21 , 183,
200, 6 , 112, 157, 65 , 117, 25 , 201,
170, 252, 77 , 191, 42 , 115, 132, 213,
195, 175, 43 , 134, 167, 177, 178, 91 ,
70 , 211, 159, 253, 212, 15 , 156, 47 ,
155, 67 , 239, 217, 121, 182, 83 , 127,
193, 240, 35 , 231, 37 , 94 , 181, 30 ,
162, 223, 166, 254, 172, 34 , 249, 226,
74 , 188, 53 , 202, 238, 120, 5 , 107,
81 , 225, 89 , 163, 242, 113, 86 , 17 ,
106, 137, 148, 101, 140, 187, 119, 60 ,
123, 40 , 171, 210, 49 , 222, 196, 95 ,
204, 207, 118, 44 , 184, 216, 46 , 54 ,
219, 105, 179, 20 , 149, 190, 98 , 161,
59 , 22 , 102, 233, 92 , 108, 109, 173,
55 , 97 , 75 , 185, 227, 186, 241, 160,
133, 131, 218, 71 , 197, 176, 51 , 250,
150, 111, 110, 194, 246, 80 , 255, 93 ,
169, 142, 23 , 27 , 151, 125, 236, 88 ,
247, 31 , 251, 124, 9 , 13 , 122, 103,
69 , 135, 220, 232, 79 , 29 , 78 , 4 ,
235, 248, 243, 62 , 61 , 189, 138, 136,
221, 205, 11 , 19 , 152, 2 , 147, 128,
144, 208, 36 , 52 , 203, 237, 244, 206,
153, 16 , 68 , 64 , 146, 58 , 1 , 38 ,
18 , 26 , 72 , 104, 245, 129, 139, 199,
214, 32 , 10 , 8 , 0 , 76 , 215, 116 };
void initK(byte* inKey); //подается ключ 32 байта
static void LSX(byte* key, const byte* arg); //процесс функций L, S и Х: подаются 16 байт ключа
static void L(byte*); //L преобразование, на вход получает массив 16 байт: в одном случае номер
//элемента i в виде массива, в другом случае массив после R преобразования
static void F(byte* masC, byte* x, byte* y); //F преобразование, на входе массив итерационных констант masC 16 байт,
//массивы по 16 байт х и у, которые являются разложением ключа 32 байта
//на К1 и К2 по 16 байт
static void S(byte* masLSX_XSL); //преобразование S, которому на вход поступает массив из 16 байт после Х
static byte mul_gf256(byte x, byte y); //функция mul
static void R(byte* arg); //преобразование R, на вход поступает массив из 16 байт
static byte l(byte* arg); //преобразование l
// массив из l подстановок
const byte lNumbers[] = {
148, 32 , 133, 16 , 194, 192, 1 , 251,
1 , 192, 194, 16 , 133, 32 , 148, 1 };
static byte iterK[10][16];
static byte masC[32][16];
static byte masLSX_XSL[16];
static bool isMasC = false;
static void GET_MAS_C() {
for(int i = 0; i < 32; i++) {
memset(masC[i], 0, 15);
masC[i][15] = (byte) (i + 1);
L(masC[i]);
}
}
void init128Gost34_12_15(byte* key) {
if (!isMasC) {
GET_MAS_C();
isMasC = true;
}
initK(key);
}
void initK(byte* inKey) {
byte x[16];
byte y[16];
for (int i = 0; i < 16; i++) {
iterK[0][i] = x[i] = inKey[i];
iterK[1][i] = y[i] = inKey[i + 16];
}
for (int i = 1; i < 5; i++) {
int q = 0;
for (int j = 1; j < 9; j++) {
F(masC[(8 * (i - 1) + j) - 1], x, y);
}
arraycopy(x, 0, iterK[2 * i], 0, 16);
arraycopy(y, 0, iterK[2 * i + 1], 0, 16);
}
}
void close128Gost34_12_15(){
for (int i = 0; i < 32; i++)
memset(masC[i], 0, 16);
for (int i = 0; i < 10; i++)
memset(iterK[i], 0, 16);
}
void encrypt128Gost34_12_15(byte* text){
byte block[16];
arraycopy(text, 0, block, 0, 16);
for (int i = 0; i < 9; i++) {//длина 16 байт
LSX(iterK[i], block);
memmove(block, masLSX_XSL, 16);
}
X(block, iterK[9]);
arraycopy(block, 0, text, 0, 16);
}
static void F(byte* key, byte* arg1, byte* arg0) {
LSX(key, arg1);
X(masLSX_XSL, arg0);
arraycopy(arg1, 0, arg0, 0, 16);
arraycopy(masLSX_XSL, 0, arg1, 0, 16);
}
static void LSX(byte* key, const byte* arg) {
memmove(masLSX_XSL, key, 16);
X(masLSX_XSL, arg);
S(masLSX_XSL);
L(masLSX_XSL);
}
static void S(byte* arg) {
for (int i = 15; i >= 0; i--)
arg[i] = pi[arg[i]];
}
static byte l(byte* arg) {
byte sum = arg[15];
for (int i = 14; i >= 0; i--)
sum ^= mul_gf256(arg[i], lNumbers[i]);
return sum;
}
static void R(byte* arg) {
byte z = l(arg);
byte buffer[16] = { 0 };
arraycopy(arg, 0, buffer, 1, 15);
arraycopy(buffer, 1, arg, 1, 15);
arg[0] = z;
}
static void L(byte* arg) {
for (int i = 0; i < 16; i++)
R(arg);
}
void X(byte* arg1, const byte* arg2) {
for (int i = 0; i < 16; i++)
arg1[i] ^= arg2[i];
}
static byte mul_gf256(byte x, byte y)
{
byte z = 0x0;
while (y)
{
if (y & 1)
z ^= x;
x = (x << 1) ^ (x & 0x80 ? 0xC3 : 0x00);
y >>= 1;
}
return z;
}
void arraycopy(byte* masIn, int pos1, byte* masOut, int pos2, int length) {
for (int i = pos1; i < (length + pos1); i++) {
//masIn[i] = masOut[pos2];
masOut[pos2] = masIn[i];
pos2++;
}
}
