2010-02-08 63 views
1

我有我的应用程序的一些代码,在Android中创建一个密钥存储, 创建一些默认密钥,然后保存密钥库。KeyStore加载导致Android上的EOFException错误

后来的代码尝试重新加载密钥库,在模拟器上这个 运行良好,但是当在手机上运行时,我得到一个EOFException。任何 关于我哪里出错的建议?

代码的出取低于:

在I类有下列变量

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; 
     } 
+0

哪一行引发EOFException? – 2010-02-08 12:05:34

+0

没关系排序,事先发生另一个异常,这意味着无论密钥存储为空。 – 2010-02-08 12:25:39

+0

密钥库甚至是空的 – 2010-02-08 12:25:59

回答

0

这通常发生在当密钥存储文件已损坏。我们刚刚(又一次)有一个外部开发者发送的密钥库的问题,他无法回想起他是如何创建密钥库文件的。

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