using RNCryptor on android

2.7k views Asked by At

I have some image files that are encrypted with RNCryptor and I want to decrypt them and use them as a background of a Framelayout. I walk through sample files, but my application force closes after a while and didn't show any images. I use JNCryptor library

I put encrypted image in raw folder as below:

here my project folder

and this is my code in my activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FrameLayout img = (FrameLayout) findViewById(R.id.scrollView);

    final String password = "myPasswordString";

    JNCryptor cryptor = new AES256JNCryptor();

    Resources res = getResources();
    InputStream in_s = res.openRawResource(R.raw.troodon_ph);
    byte[] b = null;
    byte[] data = null;
    try {
        b = new byte[in_s.available()];
        in_s.read(b);
    } catch (IOException e) {
        Log.i("decrypt error", e.toString());
    }

    try {
        data = cryptor.decryptData(b, password.toCharArray());
    } catch (InvalidHMACException e) {
        Log.i("decrypt error", e.toString());
    } catch (CryptorException e) {
        Log.i("decrypt error", e.toString());
    }

    Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    BitmapDrawable bDrawable = new BitmapDrawable(res, mBitmap);

    img.setBackgroundDrawable(bDrawable);
}

This file works great on iOS with this code:

NSString  *imagePath = [[NSBundle mainBundle] pathForResource:getName ofType:@"wod"];
NSData *encryptedData = [NSData dataWithContentsOfFile:imagePath];
NSData *decryptedData = [RNDecryptor decryptData:encryptedData
                                    withPassword:PASSWORD
                                           error:&error];

UIImage*  bgImage = [UIImage imageWithData:decryptedData];
UIImageView * movingImageView = [[UIImageView alloc]initWithImage:bgImage];

And here is logcat output:

08-28 19:52:11.720: E/AndroidRuntime(1063): FATAL EXCEPTION: main 08-28 19:52:11.720: E/AndroidRuntime(1063): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.appersian.android.wod/net.appersian.android.wod.MainActivity}: java.lang.NullPointerException 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.ActivityThread.access$600(ActivityThread.java:130) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.os.Handler.dispatchMessage(Handler.java:99) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.os.Looper.loop(Looper.java:137) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.ActivityThread.main(ActivityThread.java:4745) 08-28 19:52:11.720: E/AndroidRuntime(1063): at java.lang.reflect.Method.invokeNative(Native Method) 08-28 19:52:11.720: E/AndroidRuntime(1063): at java.lang.reflect.Method.invoke(Method.java:511) 08-28 19:52:11.720: E/AndroidRuntime(1063): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 08-28 19:52:11.720: E/AndroidRuntime(1063): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 08-28 19:52:11.720: E/AndroidRuntime(1063): at dalvik.system.NativeStart.main(Native Method) 08-28 19:52:11.720: E/AndroidRuntime(1063): Caused by: java.lang.NullPointerException 08-28 19:52:11.720: E/AndroidRuntime(1063): at net.appersian.android.wod.MainActivity.onCreate(MainActivity.java:54) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.Activity.performCreate(Activity.java:5008) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 08-28 19:52:11.720: E/AndroidRuntime(1063): ... 11 more

What am I doing wrong about decrypting the file?

update: i added e.printStackTrace() to try/catch, and here is my new logcat:

08-28 20:54:10.496: W/System.err(1487): org.cryptonode.jncryptor.InvalidHMACException: Incorrect HMAC value. 08-28 20:54:10.496: W/System.err(1487): at org.cryptonode.jncryptor.AES256JNCryptor.decryptV3Data(AES256JNCryptor.java:244) 08-28 20:54:10.496: W/System.err(1487): at org.cryptonode.jncryptor.AES256JNCryptor.decryptV3Data(AES256JNCryptor.java:319) 08-28 20:54:10.496: W/System.err(1487): at org.cryptonode.jncryptor.AES256JNCryptor.decryptData(AES256JNCryptor.java:276) 08-28 20:54:10.496: W/System.err(1487): at net.appersian.android.wod.MainActivity.onCreate(MainActivity.java:50) 08-28 20:54:10.496: W/System.err(1487): at android.app.Activity.performCreate(Activity.java:5008) 08-28 20:54:10.496: W/System.err(1487): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 08-28 20:54:10.496: W/System.err(1487): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 08-28 20:54:10.496: W/System.err(1487): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 08-28 20:54:10.496: W/System.err(1487): at android.app.ActivityThread.access$600(ActivityThread.java:130) 08-28 20:54:10.496: W/System.err(1487): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 08-28 20:54:10.496: W/System.err(1487): at android.os.Handler.dispatchMessage(Handler.java:99) 08-28 20:54:10.496: W/System.err(1487): at android.os.Looper.loop(Looper.java:137) 08-28 20:54:10.496: W/System.err(1487): at android.app.ActivityThread.main(ActivityThread.java:4745) 08-28 20:54:10.496: W/System.err(1487): at java.lang.reflect.Method.invokeNative(Native Method) 08-28 20:54:10.496: W/System.err(1487): at java.lang.reflect.Method.invoke(Method.java:511) 08-28 20:54:10.496: W/System.err(1487): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 08-28 20:54:10.496: W/System.err(1487): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 08-28 20:54:10.496: W/System.err(1487): at dalvik.system.NativeStart.main(Native Method)

2

There are 2 answers

4
Ali Abdolahi On BEST ANSWER

i work a lot on this problem, i asked developer on Github and at last, i found that the problem was password string and not because of wrong password, is because of unicode character in password string, in some reason, if we want to get encoding/decoding work great on all device and platform, is better to choose ascii character for password.

2
mmlooloo On

first:

http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available%28%29

http://docs.oracle.com/javase/8/docs/technotes/guides/io/troubleshooting.html

Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream

i am not sure those make any problem but lets try this :

    InputStream is = getResources().openRawResource(R.raw.troodon_ph);
    BufferedInputStream bis = new BufferedInputStream(is);
    ByteArrayBuffer baf = new ByteArrayBuffer(50);
    int current = 0;
    try {
        while ((current = bis.read()) != -1) {

            baf.append((byte) current);

        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    byte[] data = baf.toByteArray();

i hope it works but let me know!!

and at the end if it dose not work, make sure your file is encrypted correctly.