在android中存储凭据

时间:2015-07-30 18:11:09

标签: android passwords username

我应该如何在我为自己创建的应用中存储用户凭据?我没有设置任何服务器,我只是通过USB将应用程序直接安装到我的手机上。我想要做的是让自己输入用户名/密码来关联到帐户,基本上与大多数其他应用程序相同。唯一的区别是我没有设置任何服务器,因为我是新的,不知道该怎么做。所以考虑到这一点,我可以逃脱存储在数据库中并从那里拉取信息,或者,正如我所知道的,是否有一种更简单的方法来实现这一点由android提供?

注意:我很新,我厌倦了书本学习,所以我只是在增长知识,因为我需要它来构建应用程序。我基本上都在寻找最简单的技术来实现这一目标,并最终在以后进行扩展。谢谢你们,我非常感谢你们的帮助!

3 个答案:

答案 0 :(得分:0)

可以使用SharedPreferences ..

来完成
SharedPreferences wmbPreference1,wmbPreference2;    
SharedPreferences.Editor editor;

//wmbPreference for Shared Prefs that lasts forever
wmbPreference1 = PreferenceManager.getDefaultSharedPreferences(this);  

//save_item for Shared Prefs that lasts only just once each time program is running. It is just a name given.
wmbPreference2 =getApplicationContext().getSharedPreferences("save_item",Activity.MODE_PRIVATE);

保存值

SharedPreferences.Editor editor = wmbPreference1.edit();
editor.putString("MYKEY", "12345");
editor.commit();

您可以检索

之类的值
String Phonenumber = wmbPreference1.getString("MYKEY", ""); 

其中 MYKEY 是您可以识别该值的键名..

在文档herehere

中详细了解相关信息

<强>更新

正如@adelphus在评论中提到的,建议在保存之前加密密码。

答案 1 :(得分:0)

在我的情况下,我将用户名和密码存储在sharedprefrences中,但不是通常的和不安全的。 除了@Lal回答,我建议你使用secureSharedPrefrences来完成这个伟大的工作和来自scottyab的安全方法,ok开始将这个依赖项添加到你的gradle:

com.scottyab:secure-preferences-lib:0.1.1

来自此lib的额外信息:

  

这是Android共享首选项包装器,它使用AES 128,CBC和PKCS5填充加密共享首选项的值,并以SHA 256哈希的形式进行完整性检查。每个密钥都存储为单向SHA 256哈希。在存储到prefs xml文件之前,键和值都是base64编码的。默认情况下,生成的密钥存储在后备首选项文件中,因此可以由root用户读取和提取。

所以在向gradle添加依赖项后,使用以下代码段创建类:

public class App extends Application {
protected static App instance;
private SecurePreferences secureAppData;

public App(){
    super();
    instance = this;
}
public static App get() {
    return instance;
}

public SharedPreferences getSharedPreferences() {
    if(secureAppData==null){
        secureAppData = new SecurePreferences(this, null, "my_prefs.xml");
        SecurePreferences.setLoggingEnabled(true);
    }
    return secureAppData;
}

}

在任何活动中都使用此代码段作为商店密钥

SharedPreferences secureAppData = App.get().getSharedPreferences();
        secureAppData.edit().putString("key",myKey)
                .commit();

并且检索使用此

secureAppData.getString("key",null)

对于那些询问应用程序类和理论的人: 有时您希望存储数据,例如需要从多个活动访问的全局变量 - 有时在应用程序中的任何位置。在这种情况下,Application对象将为您提供帮助。

也许这有助于某人。

答案 2 :(得分:0)

在回复my comment时(因为我无法在任何其他SO问题中找到一个简单的示例),这是一些用于散列,存储和检查凭据的注释代码。如果您想了解saltingpassword hashing,维基百科有一些很好的信息。

保存:

void saveCredentials(String username, String password) {
 /* create some random salt bytes - the value doesn't need to be secret (which is
  why we can save it) but it must be unpredictable and unique per-user */
 SecureRandom sr = new SecureRandom();
 byte[] salt = new byte[16];
 sr.nextBytes(salt);

  // hash the (salt + password)
  // hashing algorithms vary, but for now, SHA256 is a reasonable choice
  try {
     MessageDigest hasher = MessageDigest.getInstance("SHA-256");
     hasher.update(salt);
     hasher.update(password.getBytes("UTF-8"));
     byte[] hashedbytes = hasher.digest();

     // we can now save the salt and the hashed bytes to a file,
     //  SharedPreference or any other storage location
     savedata(username, salt, hashedbytes);

  } catch (Exception e) {
     // do something sensible on errors
  }

}

检查:

boolean checkPassword(String username, String password) {
  // read the info for the user that we saved in storage
  byte[] salt = readdata(username, "salt");
  byte[] correcthash = readdata(username, "pwdhash");

  // hash the password we are checking in the same way that we did
  // for the original password
  try {
     MessageDigest hasher = MessageDigest.getInstance("SHA-256");
     hasher.update(salt);
     hasher.update(password.getBytes("UTF-8"));
     byte[] testhash = hasher.digest();

     // if the password is correct, the two hashed values will match
     // - if it's wrong, the hashed values will have one or more
     // bytes that do not match
     for (int i=0; i < testhash.length; i++) {
         if (testhash[i] != correcthash[i])
             return false;  // mismatch - wrong password
     }

     // if we reach here, all the hash bytes match, so the password
     // matches the original
     return true;

  } catch (Exception e) {
     // do something sensible on errors
  }

  return false;
}