1

I'm trying to read in an X509 public key that is stored in pem format. I believe that this file is stored in plain text, so I don't understand why this error would be thrown:

java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed

The offending line of code is the AssetFileDescriptor in my onCreate method:

AssetFileDescriptor afd = context.getResources().openRawResourceFd(R.raw.public_key);
FileInputStream fis = afd.createInputStream();
BufferedReader reader = new BufferedReader((new InputStreamReader(fis)));

The file public_key is stored in my res/raw folder:enter image description here

The full stack trace is this:

02-15 19:58:04.440  27194-27194/? E/MYAPP﹕ exception
    android.content.res.Resources$NotFoundException: File res/raw/public_key from drawable resource ID #0x7f050000
            at android.content.res.Resources.openRawResourceFd(Resources.java:1127)
            at com.m.Crypto.EncryptRSA.getPublicKeyFromPemFormat(EncryptRSA.java:136)
            at com.m.RSATest.MainActivity.generateSessionKey(MainActivity.java:127)
            at com.m.RSATest.MainActivity.onCreate(MainActivity.java:75)
            at android.app.Activity.performCreate(Activity.java:5275)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
            at android.content.res.AssetManager.openNonAssetFdNative(Native Method)
            at android.content.res.AssetManager.openNonAssetFd(AssetManager.java:436)
            at android.content.res.Resources.openRawResourceFd(Resources.java:1124)
            at com.m.Crypto.EncryptRSA.getPublicKeyFromPemFormat(EncryptRSA.java:136)
            at com.m.RSATest.MainActivity.generateSessionKey(MainActivity.java:127)
            at com.m.RSATest.MainActivity.onCreate(MainActivity.java:75)
            at android.app.Activity.performCreate(Activity.java:5275)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
            at dalvik.system.NativeStart.main(Native Method)
AndroidDev
  • 20,466
  • 42
  • 148
  • 239
  • `FileNotFoundException` has nothing to do with key encoding. You might want to look at [Where to place Assets folder in Android Studio](http://stackoverflow.com/q/18302603/608639). – jww Feb 16 '15 at 03:09
  • @jww - Moving the public key file into the Assets folder solved my issue, with a little code revision. I'm very confused about when to put things in raw vs when to put them in assets. If you post as an answer I will accept it. Thank you! – AndroidDev Feb 16 '15 at 03:24
  • @Randall - no big deal. Answer your question with the details you used to solve it. You will provide more useful information than me :) – jww Feb 16 '15 at 03:26

1 Answers1

0

With full credit to @jww, I resolved this by moving the public key file over to the assets folder. That necessitated changing the code to this:

AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("public_key");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

And that got me past the error that I posted about. From there, it was just a matter of reading the file with the BufferedReader.

Any comments about this are appreciated.

Thanks, @jww!

AndroidDev
  • 20,466
  • 42
  • 148
  • 239