2014-03-05 50 views
0

当我尝试在keyStore中加载(KeyStore方法)以前保存的数据时,我收到EOFException。创建文件并保存数据(从KeyStore存储方法)。我想知道我应该在代码中更改以从文件中获取数据。 保存(创建一个文件,创建入口等):未从文件初始化的KeyStore

@Override 
    public boolean savePIN(Context context, int pin) throws StorageAlreadyInitializedException { 
     boolean containKey; 
     OutputStream outputStream = null; 
     if (mKeyStore == null) { 
      initializeKeyStore(context); 
     } 
     if (mKeyStore != null) { 
      try { 
       containKey = mKeyStore.containsAlias(PASSWORD_ALLIAS); 
       //if key is in keystore then throw exception 
       if (containKey) { 
        throw new StorageAlreadyInitializedException(); 
       } 
       SecretKey secretKey = KeyGenerator.getInstance(ALGORITHM_TYPE).generateKey(); 
       KeyStore.PasswordProtection password = new KeyStore.PasswordProtection((pin + "").toCharArray()); 
       KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(secretKey); 

       Log.v(TAG, "Secret key" + secretKey.toString() + " Secret Key encoded " + secretKey.getEncoded().toString()); 
       mKeyStore.setEntry(PASSWORD_ALLIAS, secretKeyEntry, password); 
      // mKeyStore.setKeyEntry(PASSWORD_ALLIAS,pinByte,null); 
       File file = new File(context.getFilesDir().getAbsolutePath(), KEYSTORE_NAME); 
       if (!file.exists()) { 
        file.createNewFile(); 
       } 
       outputStream = new FileOutputStream(file); 

       ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 
       // FileOutputStream 
       mKeyStore.store(bOut, (pin + "").toCharArray()); 
       if (bOut != null) { 
        outputStream.write(bOut.toByteArray()); 
       } 
       // Log.v(TAG, "savePin " + (pin + "").toCharArray()); 
       Key key = mKeyStore.getKey(PASSWORD_ALLIAS, (pin + "").toCharArray()); 
       Log.v(TAG, "output stream: " + outputStream.toString()); 
       Log.v(TAG, "Created key: " + key.getEncoded().toString()); 
      } catch (... e) { 
       More exceptions... 

加载:

private void initializeKeyStore(Context context, int pin) { 
     InputStream inputStream = null; 
     try { 
      mKeyStore = KeyStore.getInstance("BKS"); 
      File file = new File(context.getFilesDir().getAbsolutePath(), KEYSTORE_NAME); 
      if (!file.exists()) { 
       file.createNewFile(); 
      } 
      inputStream = new FileInputStream(file); 
      BufferedReader r = new BufferedReader(new InputStreamReader(inputStream)); 
      StringBuilder total = new StringBuilder(); 
      String line; 
      while ((line = r.readLine()) != null) { 
       total.append(line); 
      } 
      mKeyStore.load(inputStream, (pin + "").toCharArray()); 
     }catch(...exceptions){} 

回答

1
  • 调用File.createNewFile()new FileOutputStream()之前都完全没有意义和浪费。
  • new FileInputStream()之前调用它会产生积极的反作用。它所要完成的是将FileNotFoundException变成EOFException.
  • ByteArrayOutputStream在这里也是毫无意义和浪费的,bOut在您测试它时不可能为空。只需直接写入文件即可。
  • 你得到的EOFException可能意味着文件是空的,这反过来可能意味着new FileOutputStream(...)抛出了一个你没有注意到的异常,否则你永远不会调用它,直到你存在这个文件在打开输入前无意义地创建它。

不要这样写代码。我建议你解决这一切并重新测试。我毫不怀疑你在这里报告的问题将会消失。 另外这个问题可能很好地表现出来,目前被所有这些掩盖了。