2010-02-08 6 views
1

J'ai du code pour mon application qui crée un fichier de clés dans Android, crée des clés par défaut puis enregistre le fichier de clés. Plus tard, le code essaie de recharger le fichier de clés, sur l'émulateur, ce fonctionne correctement, mais quand il est exécuté sur un téléphone, j'obtiens une exception EOFException. Un des conseils sur où je vais mal?Chargement de KeyStore provoquant une erreur EOFException sur Android

Un hors prendras du code est ci-dessous:

Dans la classe je les variables suivantes

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

Ensuite, les deux fonctions suivantes

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

Quelle ligne renvoie l'exception EOFException? –

+0

Ne faites jamais l'objet d'un tri, une autre exception se produisait auparavant, ce qui signifiait que le keystore serait nul. –

+0

keystore serait vide même –

Répondre

0

Cela se produit généralement lorsque le keystore le fichier est corrompu. Nous venons d'avoir (encore) un tel problème avec un keystore envoyé par un développeur externe qui était incapable de se rappeler comment exactement il a créé le fichier keystore.

Créez un nouveau fichier de magasin de clés et importez le (s) certificat (s) - cela résoudra le problème.

Questions connexes