Witam serdecznie. Napotkałem taki problem z tworzeniem lub odczytywaniem pliku w Androidzie. Otóż na moim Androidzie 4.0.3 wszystko działa okej, jednak na Androidzie 4.4 i wyższym juz nie chce działać.
Oto jak zapisuje plik:
if(!Security.checkLogin(this._login.getText().toString()) || !Security.checkPassword(this._password.getText().toString()) || ( this._password.getText().toString().compareTo(this._passwordconfirm.getText().toString()) != 0 ))
{
this._warning.setText(R.string.CDB_Warning);
}
else
{
PasswordDatabase nowa = new PasswordDatabase();
FileOutputStream fos = null;
try {
fos = this.getApplicationContext().openFileOutput(this._login.getText().toString() + ".db",MODE_WORLD_WRITEABLE);
Security.encrypt(nowa,fos,this._login.getText().toString(), this._password.getText().toString());
fos.close();
this.finish();
} catch (IOException e) {
Log.e("Błąd","openFileOutput\r\n");
}
}
tak odczytuje
_db = null;
FileInputStream fis = null;
try {
fis = openFileInput(this.login.getText().toString() + ".db");
_db = (PasswordDatabase) com.example.tomus.passwordwalletbeta.Security.decrypt(fis, this.login.getText().toString(), this.password.getText().toString());
fis.close();
if(_db==null){
this.info.setText("Podano nieprawidłowe informacje.");
}
else {
Intent intent = new Intent(MainActivity.this, LoggedActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("db", _db);
bundle.putString("login", login.getText().toString());
bundle.putString("password", password.getText().toString());
intent.putExtras(bundle);
this.login.setText("");
this.password.setText("");
startActivity(intent);
}
} catch (InvalidKeyException | NoSuchPaddingException | NoSuchAlgorithmException e) {
this.info.setText("Podano nieprawidłowe informacje.");
} catch (FileNotFoundException e) {
this.info.setText("Podano nieprawidłowe informacje. FileNotFound");
} catch (IOException e) {
this.info.setText("Podano nieprawidłowe informacje. IOException");
}
}
Tutaj metody encrypt i decrypt wykorzystywane przy odczycie i zapisie pliku.
public static void encrypt(PasswordDatabase object, OutputStream ostream, String login, String password){ try{ SecretKeySpec sks = new SecretKeySpec(generateKey(login,password),transformation);
Cipher cipher = Cipher.getInstance(transformation);
cipher.init(Cipher.ENCRYPT_MODE,sks);
SealedObject sealedobject = new SealedObject(object,cipher);
CipherOutputStream cos = new CipherOutputStream(ostream,cipher);
ObjectOutputStream outputStream = new ObjectOutputStream(cos);
outputStream.writeObject(sealedobject);
outputStream.close();
cos.close();
}catch(Exception e){
}
}
public static Object decrypt(InputStream istream, String login, String password) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IOException{ SecretKeySpec sks = new SecretKeySpec(generateKey(login,password),transformation); Cipher cipher = Cipher.getInstance(transformation); cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cipherInputStream = new CipherInputStream(istream, cipher);
ObjectInputStream inputStream = new ObjectInputStream(cipherInputStream);
SealedObject sealedobject;
try{
sealedobject = (SealedObject) inputStream.readObject();
inputStream.close();
cipherInputStream.close();
return sealedobject.getObject(cipher);
}catch(IOException | ClassNotFoundException | IllegalBlockSizeException | BadPaddingException e){
Log.e("Błąd", "Jakis Exception....");
return null;
}
Czy ktoś ma jakiś pomysł, co może być przyczyną działania tego u mnie, a na innych telefonach nie?