简体   繁体   中英

Android app login screen - wrong variable and shared preferences comparison

I'm trying to store encrypted password in SharedPreferences of my app. According to logs password is saved correctly and matches typed password after encryption, but in the if comparison they aren't equal. Every time it runs else instruction. I can't find what's wrong so I'm asking for help! BTW what would be better, more secure way of storing password, even encrypted?

public class LoginActivity extends AppCompatActivity {

    private static final String PREFERENCES_NAME = "passHashed";
    private static final String PREFERENCES_TEXT_FIELD = "textField";

    public EditText Password;
    public TextView Info;
    private String passSavedHash;

    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        Password = (EditText) findViewById(R.id.etPassword);
        Info = (TextView) findViewById(R.id.Info);
        Button loginBtn = findViewById(R.id.loginButton);

        preferences = getSharedPreferences(PREFERENCES_NAME, AppCompatActivity.MODE_PRIVATE);

        if(!passRead().equals("0")) {
            passSavedHash = passRead();
            Log.d("Password", "passHashed: " + passSavedHash);
        }
        else{
            loginBtn.setText(R.string.set_pass);
        }
    }


    public void onClickValidate(View view) throws Exception {
        String password = Password.getText().toString();
        String passHash = CipherAlgorithm.encrypt(password);

        Log.d("Password", "passHashed:    " + passSavedHash);
        Log.d("Password", "passSavedHash: " + passHash);

        if(passRead().equals("0") && password.length() > 3) {
            passSave();
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
        }
        else {
            if (Objects.equals(passSavedHash, passHash)) {
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
            } else {
                Info.setText(R.string.wron_pass);
            }
        }
        Password.setText("");

    }

    private void passSave() throws Exception {
        SharedPreferences.Editor preferencesEditor = preferences.edit();
        String editTextData = CipherAlgorithm.encrypt(Password.getText().toString());
        preferencesEditor.putString(PREFERENCES_TEXT_FIELD, editTextData);
        preferencesEditor.commit();
    }

    private String passRead() {
        return preferences.getString(PREFERENCES_TEXT_FIELD, "0");
    }

}

Ok, I figured it out. It was problem with whitespace. I added trim() method to passwords strings and now it works fine!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM