From 34013b4891b3bf9ef49e2df63b2e3f980f9d10fe Mon Sep 17 00:00:00 2001 From: Hoang Hiep MAC Date: Mon, 24 Oct 2016 19:11:49 +0700 Subject: [PATCH 1/2] Fix bug RNCryptorError error 2 in ios, when encryptor in Android --- rncryptor-native/src/main/jni/rncryptor.cpp | 3 ++- rncryptor-native/src/main/jni/rncryptor.h | 2 +- rncryptor-native/src/main/jni/rnencryptor.h | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/rncryptor-native/src/main/jni/rncryptor.cpp b/rncryptor-native/src/main/jni/rncryptor.cpp index 5186b0c..b586df6 100755 --- a/rncryptor-native/src/main/jni/rncryptor.cpp +++ b/rncryptor-native/src/main/jni/rncryptor.cpp @@ -35,7 +35,7 @@ using CryptoPP::Base64Encoder; using CryptoPP::Base64Decoder; RNCryptor::RNCryptor() { - configureSettings(SCHEMA_2); + configureSettings(SCHEMA_3); } void RNCryptor::configureSettings(RNCryptorSchema schemaVersion) @@ -59,6 +59,7 @@ void RNCryptor::configureSettings(RNCryptorSchema schemaVersion) break; case SCHEMA_2: + case SCHEMA_3: aesMode = MODE_CBC; options = OPTIONS_1; hmac_includesHeader = true; diff --git a/rncryptor-native/src/main/jni/rncryptor.h b/rncryptor-native/src/main/jni/rncryptor.h index d3b1a24..c4ce886 100755 --- a/rncryptor-native/src/main/jni/rncryptor.h +++ b/rncryptor-native/src/main/jni/rncryptor.h @@ -27,7 +27,7 @@ enum RNCryptorAlgorithm { }; enum RNCryptorSchema { - SCHEMA_0, SCHEMA_1, SCHEMA_2 + SCHEMA_0, SCHEMA_1, SCHEMA_2, SCHEMA_3 }; enum RNCryptorOptions { diff --git a/rncryptor-native/src/main/jni/rnencryptor.h b/rncryptor-native/src/main/jni/rnencryptor.h index c1d8cdc..61b8dd0 100755 --- a/rncryptor-native/src/main/jni/rnencryptor.h +++ b/rncryptor-native/src/main/jni/rnencryptor.h @@ -11,7 +11,7 @@ class RNEncryptor : public RNCryptor { string generateSalt(); public: - string encrypt(string plaintext, string password, RNCryptorSchema schemaVersion = SCHEMA_2); + string encrypt(string plaintext, string password, RNCryptorSchema schemaVersion = SCHEMA_3); }; #endif From 7229ebeb7e3fd20c7375e16c8b9280d0d7880171 Mon Sep 17 00:00:00 2001 From: Hoang Hiep MAC Date: Mon, 7 Nov 2016 10:54:05 +0700 Subject: [PATCH 2/2] Use byte array instead of string --- rncryptor-native/src/main/jni/rncryptor-native.cpp | 10 +++++++--- rncryptor-native/src/main/jni/rncryptor-native.h | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/rncryptor-native/src/main/jni/rncryptor-native.cpp b/rncryptor-native/src/main/jni/rncryptor-native.cpp index d0e6293..57465ba 100644 --- a/rncryptor-native/src/main/jni/rncryptor-native.cpp +++ b/rncryptor-native/src/main/jni/rncryptor-native.cpp @@ -84,8 +84,9 @@ jbyteArray Java_tgio_rncryptor_RNCryptorNative_encrypt(JNIEnv *env, jobject inst } -jstring Java_tgio_rncryptor_RNCryptorNative_decrypt(JNIEnv *env, jobject instance, jstring encrypted_, jstring password_) { - string decrypted = "0"; +jbyteArray Java_tgio_rncryptor_RNCryptorNative_decrypt(JNIEnv *env, jobject instance, + jstring encrypted_, jstring password_) { + std::string decrypted; if (encrypted_ != NULL) { try { const char *encrypted = env->GetStringUTFChars(encrypted_, 0); @@ -95,9 +96,12 @@ jstring Java_tgio_rncryptor_RNCryptorNative_decrypt(JNIEnv *env, jobject instanc delete cryptor; env->ReleaseStringUTFChars(encrypted_, encrypted); env->ReleaseStringUTFChars(password_, password); + jbyteArray array = env->NewByteArray((jsize) decrypted.size()); + env->SetByteArrayRegion(array, 0, (jsize) decrypted.size(), (const jbyte *) decrypted.c_str()); + return array; } catch (exception e) { decrypted = "error decrypting"; } } - return env->NewStringUTF(decrypted.c_str()); + return NULL; } \ No newline at end of file diff --git a/rncryptor-native/src/main/jni/rncryptor-native.h b/rncryptor-native/src/main/jni/rncryptor-native.h index 14f99f1..e7a7f55 100644 --- a/rncryptor-native/src/main/jni/rncryptor-native.h +++ b/rncryptor-native/src/main/jni/rncryptor-native.h @@ -25,7 +25,7 @@ extern "C" { #endif JNIEXPORT jstring JNICALL Java_tgio_rncryptor_RNCryptorNative_getABI(JNIEnv* env, jobject thiz); JNIEXPORT jbyteArray JNICALL Java_tgio_rncryptor_RNCryptorNative_encrypt(JNIEnv *env, jobject instance, jstring raw_, jstring password_); - JNIEXPORT jstring JNICALL Java_tgio_rncryptor_RNCryptorNative_decrypt(JNIEnv *env, jobject instance, jstring encrypted_, jstring password_); + JNIEXPORT jbyteArray JNICALL Java_tgio_rncryptor_RNCryptorNative_decrypt(JNIEnv *env, jobject instance, jstring encrypted_, jstring password_); #ifdef __cplusplus } #endif