gpt4 book ai didi

java - Android 应用程序登录屏幕 - 错误变量和共享首选项比较

转载 作者:行者123 更新时间:2023-12-02 10:54:46 25 4
gpt4 key购买 nike

我正在尝试将加密密码存储在我的应用程序的 SharedPreferences 中。根据日志,密码已正确保存并与加密后输入的密码匹配,但在 if 比较中它们不相等。每次运行其他指令时。我找不到问题所在,所以我寻求帮助!顺便说一句,什么是更好、更安全的密码存储方式,甚至是加密的?

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

}

最佳答案

好吧,我明白了。这是空白的问题。我向密码字符串添加了trim()方法,现在它工作正常了!

关于java - Android 应用程序登录屏幕 - 错误变量和共享首选项比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51856670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com