mardi 30 juin 2015

Android 4.4 doesnt create a file, but all works fine in Android 4.0.3

i have problem with creating file in Android. I create file in my CreateDatabaseActivity like this:

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");
    } 


}

and open it in other activity with this code

_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");
}

}

This is the encrypt and decrypt methods:

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

So, why it works on my 4.0.3 but not on 4.4. How can I fix that?

Aucun commentaire:

Enregistrer un commentaire