search

Home  >  Q&A  >  body text

android - 新手求教JNI里的jstring转char问题

  1. Java向JNI里传一个String="1234567890123";

  2. JNI里用的C语言:const char key_string = (env)->GetStringUTFChars(env, jkey, 0);

    **error:****JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal continuation byte 0x38**;
    

在JNI里直接定义 char *key_string = "1234567890123";就没问题

全部代码如下,网上找的,用来加密的;

jstring  
Java_com_itapp_mylibrary_MyLibrary_setAES(JNIEnv *env, jobject thiz, jstring input, jstring jkey)
{
        //char *key_string = "1234567890123";
        const char *key_string = (*env)->GetStringUTFChars(env, jkey, 0);
        //(*env)->ReleaseStringUTFChars(env, jkey, key_string);
        
        const char *str = (*env)->GetStringUTFChars(env, input, NULL);    
        (*env)->ReleaseStringUTFChars(env, input, str);
        
        long strLen = ((strlen(str) / 16) * 16) + 16;                
    
        
        char *aesEnc = malloc(strLen);
        memset(aesEnc, 0, strLen);
        AES_set_encrypt_key(key_string, 256, &key);                                    
        for (i = 0; i < strlen(str); i += 16)AES_encrypt(str + i, aesEnc + i, &key);    
    
        char *basEnc = base64_encode(aesEnc, strlen(aesEnc));   
    
        char len[strLen+2];
        sprintf(len,"%d__",strLen);
        char *len_str = str_contact(len,basEnc);
    
        char *result = base64_encode(len_str, strlen(len_str));   
    
        return (*env)->NewStringUTF(env, result);
}

不懂C语言 看不出哪里的问题 在线求教!


PHPzPHPz2772 days ago737

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 17:50:17

    Take the opportunity to promote the following blog: NDK Development - JNI Basic Data and String Processing

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:50:17

    Allocate space and copy the string you need to key_string. .

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:50:17

    const char* cppMsg=env->GetStringUTFChars(msg, JNI_FALSE);
    env->ReleaseStringUTFChars(msg, cppMsg);
    
    这是我验证过有效的写法,供参考

    reply
    0
  • Cancelreply