为 android 应用程序提供 4 针密码保护

Provide 4-pin password protection to android app

我想为我的 android 应用程序添加 4 针密码保护,就像在 Evernote 应用程序中一样。用户应该可以选择启用或禁用密码保护。但是我不知道从哪里开始...

  1. 布局:我想要四个只接受 numbers.May 的框,使用 editText 是一个选项。但是我不太确定键盘。

  2. 密码存储:使用共享首选项有用吗?

  3. 如何添加重置密码的选项?我应该为它做一个小的网络服务吗?但是我的应用程序不使用互联网,更像是一个计算器。我正在使用 sqlite 来存储数据。

    有人可以简要说明我应该遵循的步骤吗?我的问题可能很愚蠢,但我是 android 的新手,我很无助。感谢您的回复。谢谢

您只需要两个屏幕,一个用于设置密码,另一个在用户每次打开应用时打开

  1. 在第一个屏幕中,使用四个带有自定义背景的水平编辑文本,这将使它们成为矩形(一个例子是this),设置输入类型"numberPassword" ,这将打开数字键盘并接受密码。还将每个编辑文本中的字符数限制为 1。用户在所有编辑文本中完成输入值后,将它们合并。

    String pass= editText1.getText().toString()+editText2.getText().toString()+.. and so on
    

并存储在 sharedpreferences 中:

SharedPreference sp=getSharedPreference("password",pass);
  1. 在第二个屏幕中制作相同的布局,当用户完成输入值时,再次组合所有四个并检查您存储在 SharedPreference 中的那个。例如:

    if(editText1.getText().toString()+editText2.getText().toString()
    +.. and so on).equals(sp.getString("password",""){
           //Intent to your main activity
      }
    
      else{
       Toast.makeText(getApplicationContext(),"Incorrect Password",Toast.LENGTH_LONG).show();
      }
    

我不喜欢上面的答案。您使用共享首选项保存未加密的 pin 字符串,您应该在每次要求用户输入时寻找一种加密字符串和解密的方法。共享首选项不会保存,通过物理访问 phone 可以轻松读取引脚。 What is the most appropriate way to store user settings in Android application

"The only area of concern here is what you're saving. Passwords are always a tricky thing to store, and I'd be particularly wary of storing them as clear text. The Android architecture is such that your application's SharedPreferences are sandboxed to prevent other applications from being able to access the values so there's some security there, but physical access to a phone could potentially allow access to the values."

我使用 BCrypt 安全地保存了我的密码。 操作简单:

String hashpw=BCrypt(yourstring,BCrypt.gensalt());

然后像往常一样在您的剪切首选项中保存该字符串。

到verify/enter pin 只需制作:

If(BCrypt.checkpw(inputString,yourSharedPreference)){
//open app
}
else{
//acces denied toast
}