java - KeyStore 加载导致 Android 上出现 EOFException 错误

标签 java android bouncycastle keystore

我的应用程序有一些代码可以在 android 中创建 keystore , 创建一些默认 key 然后保存 keystore 。

稍后代码尝试重新加载 keystore ,在模拟器上这个 运行良好,但是在手机上运行时出现 EOFException。任何 关于我哪里出错的建议?

代码摘录如下:

在类中我有以下变量

static KeyStore keyStore; 
String filenameKeyStore = "KeyStore.kstore"; 

然后是下面两个函数

    public KeyHandler(Context context) { 
                if(keyStore == null) { 
                        Log.d("KeyStore", "Keystore is null so loading"); 
                        if(initialiseKeyStore(context) == false) { 
                                // KeyStore failed to initialise 
                                Log.e("ERROR", "Store failed to initialise"); 
                        } 
                } 
        } 
private boolean initialiseKeyStore(Context context) { 
                FileInputStream input = null; 
                try { 
                        // Get an instance of KeyStore 
                        keyStore = KeyStore.getInstance("BKS"); 
                        // Load the KeyStore file 
                        try { 
                                // Try and open the private key store 
                                input = context.openFileInput(filenameKeyStore); 
                        } catch (FileNotFoundException e) { 
                                // If  the file doesn't exist then create the file, a ECDH key and 
store the key 
                                Log.w("Warning","File does not exist, creating new file"); 
                                try { 
                                        // Load the default Key Store 
                                        keyStore.load(null, null); 
                                        // Create the file 
                                        FileOutputStream output = 
context.openFileOutput(filenameKeyStore, 0); 
                                        // Reset private key 
                                        resetPrivateKey(context); 
                                        // Save the key 
                                        keyStore.store(output, "password".toCharArray()); 
                                        // Close the keystore and set the input stream 
                                        output.close(); 
                                        input = context.openFileInput(filenameKeyStore); 
                                        // Reset the keyStore 
                                        keyStore = KeyStore.getInstance("BKS"); 
                                } catch (FileNotFoundException ee) { 
                                        Log.e("ERROR", "File not found, even though we just created it"); 
                                        return false; 
                                } catch (NoSuchProviderException e1) { 
                                        // BC isn't working exit 
                                        e1.printStackTrace(); 
                                        System.exit(1); 
                                } catch (InvalidAlgorithmParameterException e1) { 
                                        Log.e("ERROR", "The algorithm used for secure algorithm is 
incorrect"); 
                                        e1.printStackTrace(); 
                                        return false; 
                                } 
                        } 
                        // Load the store 
                        keyStore.load(input, "password".toCharArray()); 
                } catch (KeyStoreException e) { 
                        // TODO Auto-generated catch block 
                        e.printStackTrace(); 
                        return false; 
                } catch (CertificateException e) { 
                        // TODO Auto-generated catch block 
                        e.printStackTrace(); 
                        return false; 
                }  catch (EOFException e) { 
                      // Where the exception is caught 
                        e.printStackTrace(); 
                        return false; 
                } catch (IOException e) { 
                        // TODO Auto-generated catch block 
                        e.printStackTrace(); 
                        return false; 
                } catch (NoSuchAlgorithmException e) { 
                        // TODO Auto-generated catch block 
                        e.printStackTrace(); 
                        return false; 
                } catch (NoSuchProviderException e) { 
                        // Serious error, return 
                        e.printStackTrace(); 
                        System.exit(1); 
                } 
                return true; 
        } 

最佳答案

这通常发生在 keystore 文件损坏时。我们刚刚(又一次)遇到了这样的问题,外部开发人员发送的 keystore 无法记忆起他究竟是如何创建 keystore 文件的。

创建一个新的 keystore 文件并导入证书 - 这将解决问题。

关于java - KeyStore 加载导致 Android 上出现 EOFException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2221351/

相关文章:

android - 如何使一个按钮将多个 TextView 提交到我的电子邮件?

android - 将视频保存为输入而不是 android 中的相机

java - 从 Bouncy CaSTLe 中的 byte[] 获取 ECPoint/ECPublicKeyParameters

java - 如何使用docx4j设置下载的word文档的名称?

java - 存储应用程序设置的最佳方式是什么? (MVC)

android - 安装 GO SMS Pro 时未调用 SMS Broadcastreceiver

java - 具有公钥的 RSA 签名作为未在 java 中验证的文本

java - 用于 fips 的 Bouncy CaSTLe 无法在 Linux 虚拟机上运行

java - 及时查找添加到 Guava LinkedHashMultimap/HashMultiMap 的第一个和最后一个元素

java - 我在哪里可以找到我保存在 android 上的文件?