0

i write a simple string encrypt and decrypt with split string to two var and increment assci code and after swap right and left of string.

when i encrypt this text = "this is test" this is work well, but when encrypt utf char don't word like this string = "تست تست تست تست"

encrypt code is :

JNIEXPORT jstring JNICALL Java_com_test_ndk_MainActivity_encrypt(JNIEnv* env, jobject thiz, jstring dec) {
const char *nativeString = (*env)->GetStringUTFChars(env, dec, 0);
char *newstr;
char *left;
char *right;
int decLenght = strlen(nativeString);
int middl = decLenght / 2;
int i;
newstr = substr(nativeString, 0, middl);
int lenght = strlen(newstr);
left = malloc(lenght);
for (i = 0; i < lenght; i++) {
    left[i] = newstr[i] + 1;
}
left[lenght] = '\0';
newstr = substr(nativeString, middl, decLenght - middl);
lenght = strlen(newstr);
right = malloc(lenght);
for (i = 0; i < lenght; i++) {
    right[i] = newstr[i] - 1;
}
right[lenght] = '\0';
strcat(right, left);
(*env)->ReleaseStringUTFChars(env, dec, nativeString);
return (*env)->NewStringUTF(env, right);
}

and decrypt code is :

JNIEXPORT jstring JNICALL Java_com_test_ndk_MainActivity_decrypt(JNIEnv* env, jobject thiz, jstring enc) {
const char *nativeString = (*env)->GetStringUTFChars(env, enc, 0);
char *newstr;
char *left;
char *right;
int encLenght = strlen(nativeString);
int middl = encLenght / 2;
int i;
if (encLenght % 2 != 0) {
    middl++;
}
newstr = substr(nativeString, 0, middl);
int lenght = strlen(newstr);
left = malloc(lenght);
for (i = 0; i < lenght; i++) {
    left[i] = (char) ((int) newstr[i] + 1);
}
left[lenght] = '\0';
newstr = substr(nativeString, middl, encLenght - middl);
lenght = strlen(newstr);
right = malloc(lenght);
for (i = 0; i < lenght; i++) {
    right[i] = (char) ((int) newstr[i] - 1);
}
right[lenght] = '\0';
strcat(right, left);
(*env)->ReleaseStringUTFChars(env, enc, nativeString);
return (*env)->NewStringUTF(env, right);
}

substr function :

char* substr(const char *source, unsigned int start, unsigned int end) {
return strndup(source + start, end);
}

Does anyone have solution .

DrYap
  • 6,525
  • 2
  • 31
  • 54
Araz Jafaripur
  • 927
  • 2
  • 12
  • 32
  • 4
    Your code has numerous problems - it overruns buffers and leaks memory. Read up on string handline in C - it's quite different from Java. Also, the +1/-1 encryption scheme that you use may break up UTF-8 characters, and therefore needs extra care. Read up on the structure of UTF-8. – Seva Alekseyev Aug 18 '13 at 14:59
  • 1
    It's easier to define encryption and decryption algorithms on byte arrays. You can easily convert Java strings to byte arrays using Java library classes. For that matter, you can easily encrypt and decrypt using Java library classes. – Tom Blodget Aug 18 '13 at 15:39

1 Answers1

0

UTF-8 is not trivial for manipulations. For your encode/decode, you can use GetStringChars() (or the more efficient, but also more restrictive GetStringCritical()) and operate the resulting 16-bit jchar array.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • which function can manipulate jchar ? i test get error for example for substring or strlen , ... everythink work with char . – Araz Jafaripur Aug 18 '13 at 23:25
  • You can use `wcslen()` and others, or better `std::wstring` class. See for example http://stackoverflow.com/a/9100041/192373. The problem is, **wchar_t** is 32 bits in NDK. But for your purposes, an easy implementation of `strlen()` for arrays of 16-bit _chracters_ should be enough. – Alex Cohn Aug 18 '13 at 23:31
  • i can't work, can you convert my code variable to work with unicode ? – Araz Jafaripur Aug 19 '13 at 12:43