以pem格式读取公钥时出错

时间:2015-02-16 03:01:46

标签: java android android-studio

我试图读取以pem格式存储的X509公钥。我相信这个文件是以纯文本格式存储的,所以我不明白为什么会抛出这个错误:

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

令人讨厌的代码行是我的onCreate方法中的AssetFileDescriptor

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

文件public_key存储在我的res / raw文件夹中:enter image description here

完整的堆栈跟踪是这样的:

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)

1 个答案:

答案 0 :(得分:0)

完全归功于@jww,我通过将公钥文件移动到assets文件夹来解决此问题。这需要将代码更改为:

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

这让我超越了我发布的错误。从那里开始,只需要使用BufferedReader读取文件。

对此有任何意见。

谢谢,@ jww!