My Function is as follows:typedef unsigned char BYTE ;
unsigned char *test = (unsigned char *)"Hi There" ;
BYTE Key[20] ;
BYTE digest[20] ;
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject
thiz )
{
memset(Key, 0x0b, 20) ;
////////Next 2
lines are for Hashing the given text
CHMAC_SHA1 HMAC_SHA1 ;
HMAC_SHA1.HMAC_SHA1(test, strlen((const char *)test), Key, sizeof(Key),
digest) ;
////////
__android_log_write(ANDROID_LOG_ERROR,"Hello-Sample",(const char
*)digest);
jstring temp = env->NewStringUTF((const
char*)digest);
return temp;
}
void hmac_sha1( unsigned char *key,
int key_length,
unsigned char *data,
int data_length,
unsigned char *digest )
which is different from what you have. So, if it keeps erroring out, just invert the arguments.
unsigned char *test = (unsigned char *)"Hi There\0" ;
is not necessary, string literals are automatically null terminated
typedef unsigned char BYTE ;
unsigned char *test = (unsigned
char *)"Hi There" ;
BYTE Key[20] ;
BYTE digest[20] ;
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject
thiz )
{
memset(Key, 0x0b, 20) ;
////////Next 2
lines are for Hashing the given text
CHMAC_SHA1 HMAC_SHA1 ;
HMAC_SHA1.HMAC_SHA1(test, strlen((const char *)test), Key, sizeof(Key),
digest) ;
////////
__android_log_write(ANDROID_LOG_ERROR,"Hello-Sample",(const char
*)digest);
jstring temp = env->NewStringUTF((const
char*)digest);
return temp;
}env->NewStringUTF((const
char*)digest); VM aborts
Can someone please tell me how can i rectify it. --
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
--
@Tim Mensch
I was able to send the byte code but i guess you showed that the return string is
0xb617318655057264e28bc0b6fb378c8ef146be00
So should I convert first from hexadecimal to byte array and then return? Cause right now i was just copying unsigned char* to bytearray
--
That's because a Key* is size 4 on that architecture. You're getting the
size of the pointer. There's no way for the compiler to know the length
of a buffer at compile-time.
BUT, more importantly, what does convert_string_to_byte do? Key hasn't
been assigned anything above, and so unless I'm missing something, your
function is likely writing to a random address in RAM.
Tim
extern "C" jbyteArray Java_com_ryan_aes_test_MainActivity_stringToJNIencode(JNIEnv* env, jobject thiz, jint length, jbyteArray jarrByte) {unsigned char plaintext[AES_BLOCK_SIZE * 4];unsigned char ciphertext[AES_BLOCK_SIZE * 4];unsigned char checktext[AES_BLOCK_SIZE * 4];const unsigned char userKey[AES_BLOCK_SIZE * 4] = "123456";const int bits = 128;AES_KEY keyin = { { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, }, 12 };AES_KEY *key = &keyin;jbyte* pJbyte = env->GetByteArrayElements(jarrByte, NULL);if(pJbyte!=NULL){memcpy(plaintext, pJbyte, length);env->ReleaseByteArrayElements(jarrByte, pJbyte, JNI_ABORT);}unsigned char* beforeEncrypt = &plaintext[0];unsigned char* afterEncrypt = &ciphertext[0];AES_set_encrypt_key(userKey, bits, key);AES_encrypt(beforeEncrypt, afterEncrypt, key);jbyteArray jarrRet = env->NewByteArray(strlen((char*) ciphertext));env->SetByteArrayRegion(jarrRet, 0, strlen((char*) ciphertext),(jbyte *) ciphertext);return jarrRet;}