-1

i am getting java.lang.Byte[] from c(jbyteArray in JNI) . How to convert this jbyteArray to string in java.

c function is

jbyteArray Java_eu_ratikal_helloc_MainActivity_getNameBytes(JNIEnv* env, jobject thiz) {


    int n=0;
    jstring na ="HelloAndroidString";
    char* p = "HelloAndroidString";
    while(*p++){
        n++;
    }
    jbyteArray arr = (*env)->NewByteArray(env, n);
    (*env)->SetByteArrayRegion(env,arr,0,n, (jbyte*)na);

    char* b = (char*)arr;
    return arr;
}

java code is

byte[] bytes = getNameBytes();

Getting compilation error like

Incompatible types Required byte[] Found java.lang.Byte[]

Prabhu M
  • 3,534
  • 8
  • 48
  • 87
  • https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String(byte[]) – Michael Feb 23 '16 at 12:22
  • make use of java.util.Arrays.toString(bye[]) – Shriram Feb 23 '16 at 12:22
  • Do you mean `new String(bytes, "UTF-8")`? – Peter Lawrey Feb 23 '16 at 12:51
  • @PeterLawrey I cannot use this because java.lang.Byte[] is different from bytes[] – Prabhu M Feb 23 '16 at 12:56
  • @PrabhuM There is no sane reason to use a `Byte[]` It uses 4x as much memory, is slower, and much harder to work with in JNI. btw `jbyteArray` is `byte[]` not a `Byte[]` – Peter Lawrey Feb 23 '16 at 12:57
  • @PeterLawrey I have edited my question. please check – Prabhu M Feb 23 '16 at 13:11
  • Have you defined `public native byte[] getNameBytes();` ? Or did you use `Byte[]` ? – Peter Lawrey Feb 23 '16 at 13:14
  • 1
    @PeterLawrey , I used public native Byte[] getNameBytes(); Now changed to byte[] and working fine. Please mention the same thing as answer I will mark it as accepted. Thanks. Also can u please answer this http://stackoverflow.com/questions/35573832/obfuscating-or-hiding-strings-in-c-file-in-android-apk – Prabhu M Feb 23 '16 at 13:17

1 Answers1

0

You need to return

public native byte[] getNamBytes();

but to convert it into a String you need to use new String e.g.

public String String getName() {
    return new String(getNameBytes(), "UTF-8");
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Can u please tell me hoe to hide or obfuscate string in c file while using ndk – Prabhu M Feb 23 '16 at 13:24
  • @PrabhuM what are you trying to do? – Peter Lawrey Feb 23 '16 at 13:26
  • I want to secure api urls and keys from reverse engineering of android apk file. So I am trying to keep these things in c file and then access in java using ndk. But even in C file(.so file) strings are easily readable after reverse engineering – Prabhu M Feb 23 '16 at 13:27
  • @PrabhuM so you need to encypt them. If you are going to encypt them, I would do that in Java, but that's because I am a Java programmer, not a C programmer. – Peter Lawrey Feb 23 '16 at 13:31
  • 1
    @PrabhuM btw no matter what you do, the strings will have to be readable at runtime, which means if I modify your program I could get it to log out these strings. The URLs are also visible to anyone sniffing the network without modifying your program. – Peter Lawrey Feb 23 '16 at 13:33
  • But how to secure auth keys then? – Prabhu M Feb 23 '16 at 13:35
  • @PrabhuM One way is to have the server send a challenge-response. The server sends a string, the code has to transform it and give the appropriate response. This way, the auth key needed each time is different, and not actually stored anywhere. – Peter Lawrey Feb 23 '16 at 13:42