我正在尝试将加密密码存储在我的应用程序的 SharedPreferences 中。根据日志密码正确保存并在加密后匹配键入的密码,但在比较中它们不相等。每次它运行 else 指令时。我找不到问题所在,所以我寻求帮助!顺便说一句,有什么更好、更安全的密码存储方式,甚至是加密的?
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("");
}
相关分类