Reputation: 1617
When I try to load (KeyStore method) previously saved data in keyStore I receive EOFException
. File is created and data was save in it (store method from KeyStore). I would like to know what I should change in code to obtain data from file.
Saving (create a file, create Entry, etc.) :
@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...
loading:
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){}
Upvotes: 0
Views: 563
Reputation: 311050
File.createNewFile()
before new FileOutputStream()
is both completely pointless and wasteful.new FileInputStream()
is positively counterproductive. All it accomplishes is turning a FileNotFoundException
into an EOFException.
ByteArrayOutputStream
is also pointless and wasteful here, and bOut
cannot possibly be null at the point you're testing it. Just write directly to the file.EOFException
you're getting probably means the file is empty, which in turn probably means that new FileOutputStream(...)
threw an exception which you haven't noticed, or else you never called it at all and the file didn't exist until you pointlessly created it before opening it for input.Don't write code like this. I suggest you fix all this and retest. I have no doubt that the problem you report here will disappear. Another problem may well then surface, masked at present by all this.
Upvotes: 1