2

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)

Ali Abdolahi
  • 324
  • 3
  • 10

2 Answers2

1

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.

mmlooloo
  • 18,937
  • 5
  • 45
  • 64
  • i test your code, and i don't get anything difference, so, i decided to encrypt a picture with my own code, and decrypt it again, and all things work great with this manner! i don't know how to fix this problem, all i think to know now! is that i have to encrypt all files again with java! if you know better encryption method, please let me know that! thanks for your support. – Ali Abdolahi Aug 29 '14 at 11:32
  • @AliAbdolahi If your problem isn't fixed, then I wouldn't mark this as accepted. It deserves a +1, since it highlighted an error in your code and *could* have been the only problem. Side note: I'm the author of JNCryptor, so I have a personal interest in seeing your problem completely fixed. – Duncan Jones Aug 29 '14 at 12:49
1

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.

Ali Abdolahi
  • 324
  • 3
  • 10
  • very good i want to encrypt images at my pc and decrypt it in my app what code or library do you use? please guide me i search and found `conceal` for facebook but it can not be used on pc or i can not know how to do that, can you tell me the link of your encryption and decryption for AES? – mmlooloo Sep 09 '14 at 09:51
  • JNCryptor was very slow on Java and Android, but, i made an independent class to encrypt and decrypt, on Android or Java(for PC), i can share it to Github, if you want – Ali Abdolahi Sep 09 '14 at 10:14
  • here it is : https://gist.github.com/aliab/8cc7c4b9f8cbda263dc7 i made a Android Helper class for easily working on android but, by the way if you want to use it in java, just convert your Fileinputstream to byte[] and use Ecryptor.java class ;) – Ali Abdolahi Sep 09 '14 at 10:49
  • @AliAbdolahi Unicode characters in your password should not be a problem. Please can you update either your question here or the GitHub issue with further information about your problem? As I mentioned in GitHub, your replacement code is flawed (with regards to key generation). – Duncan Jones Sep 22 '14 at 07:42