代码之家  ›  专栏  ›  技术社区  ›  Niko Gamulin

在Android应用程序中存储用户设置最合适的方法是什么

  •  291
  • Niko Gamulin  · 技术社区  · 15 年前

    我正在创建一个使用用户名/密码连接到服务器的应用程序,我想启用“保存密码”选项,这样用户就不必每次启动应用程序时都输入密码。

    我试着用共享的首选项来做,但不确定这是否是最好的解决方案。

    对于如何在Android应用程序中存储用户值/设置,我将不胜感激。

    14 回复  |  直到 6 年前
        1
  •  226
  •   Reto Meier    15 年前

    一般来说,sharedreferences是存储首选项的最佳选择,所以一般来说,我建议使用这种方法来保存应用程序和用户设置。

    这里唯一需要关注的是你所保存的东西。密码总是一个棘手的事情来存储,我会特别提防他们作为明文存储。Android的体系结构是这样的:应用程序的共享引用是沙盒的,以防止其他应用程序能够访问这些值,因此那里有一些安全性,但是对电话的物理访问可能允许访问这些值。

    如果可能的话,我会考虑修改服务器以使用协商的令牌来提供访问,比如 OAuth . 或者,您可能需要构造某种类型的加密存储,尽管这是非常重要的。至少,在将密码写入磁盘之前,要确保对其进行了加密。

        2
  •  204
  •   JulienGenoud    8 年前

    我同意雷托和菲泽德。客观地说,花大量时间和精力加密sharedreferences中的密码没有多大意义,因为任何有权访问您的首选项文件的攻击者也很可能访问您的应用程序的二进制文件,因此解密密码的密钥。

    然而,也就是说,似乎确实有一项宣传计划正在进行中,即识别将密码以明文形式存储在共享的引用中的移动应用程序,并对这些应用程序发出不利的光。参见 http://blogs.wsj.com/digits/2011/06/08/some-top-apps-put-data-at-risk/ http://viaforensics.com/appwatchdog 例如。

    虽然我们一般需要更多地关注安全问题,但我认为这种对这一特定问题的关注实际上并没有显著提高我们的整体安全性。然而,感知是这样的,这里有一个解决方案来加密您在sharedreferences中放置的数据。

    只需将您自己的sharedreferences对象包装在这个对象中,您读/写的任何数据都将自动加密和解密。如。

    final SharedPreferences prefs = new ObscuredSharedPreferences( 
        this, this.getSharedPreferences(MY_PREFS_FILE_NAME, Context.MODE_PRIVATE) );
    
    // eg.    
    prefs.edit().putString("foo","bar").commit();
    prefs.getString("foo", null);
    

    这是班级的代码:

    /**
     * Warning, this gives a false sense of security.  If an attacker has enough access to
     * acquire your password store, then he almost certainly has enough access to acquire your
     * source binary and figure out your encryption key.  However, it will prevent casual
     * investigators from acquiring passwords, and thereby may prevent undesired negative
     * publicity.
     */
    public class ObscuredSharedPreferences implements SharedPreferences {
        protected static final String UTF8 = "utf-8";
        private static final char[] SEKRIT = ... ; // INSERT A RANDOM PASSWORD HERE.
                                                   // Don't use anything you wouldn't want to
                                                   // get out there if someone decompiled
                                                   // your app.
    
    
        protected SharedPreferences delegate;
        protected Context context;
    
        public ObscuredSharedPreferences(Context context, SharedPreferences delegate) {
            this.delegate = delegate;
            this.context = context;
        }
    
        public class Editor implements SharedPreferences.Editor {
            protected SharedPreferences.Editor delegate;
    
            public Editor() {
                this.delegate = ObscuredSharedPreferences.this.delegate.edit();                    
            }
    
            @Override
            public Editor putBoolean(String key, boolean value) {
                delegate.putString(key, encrypt(Boolean.toString(value)));
                return this;
            }
    
            @Override
            public Editor putFloat(String key, float value) {
                delegate.putString(key, encrypt(Float.toString(value)));
                return this;
            }
    
            @Override
            public Editor putInt(String key, int value) {
                delegate.putString(key, encrypt(Integer.toString(value)));
                return this;
            }
    
            @Override
            public Editor putLong(String key, long value) {
                delegate.putString(key, encrypt(Long.toString(value)));
                return this;
            }
    
            @Override
            public Editor putString(String key, String value) {
                delegate.putString(key, encrypt(value));
                return this;
            }
    
            @Override
            public void apply() {
                delegate.apply();
            }
    
            @Override
            public Editor clear() {
                delegate.clear();
                return this;
            }
    
            @Override
            public boolean commit() {
                return delegate.commit();
            }
    
            @Override
            public Editor remove(String s) {
                delegate.remove(s);
                return this;
            }
        }
    
        public Editor edit() {
            return new Editor();
        }
    
    
        @Override
        public Map<String, ?> getAll() {
            throw new UnsupportedOperationException(); // left as an exercise to the reader
        }
    
        @Override
        public boolean getBoolean(String key, boolean defValue) {
            final String v = delegate.getString(key, null);
            return v!=null ? Boolean.parseBoolean(decrypt(v)) : defValue;
        }
    
        @Override
        public float getFloat(String key, float defValue) {
            final String v = delegate.getString(key, null);
            return v!=null ? Float.parseFloat(decrypt(v)) : defValue;
        }
    
        @Override
        public int getInt(String key, int defValue) {
            final String v = delegate.getString(key, null);
            return v!=null ? Integer.parseInt(decrypt(v)) : defValue;
        }
    
        @Override
        public long getLong(String key, long defValue) {
            final String v = delegate.getString(key, null);
            return v!=null ? Long.parseLong(decrypt(v)) : defValue;
        }
    
        @Override
        public String getString(String key, String defValue) {
            final String v = delegate.getString(key, null);
            return v != null ? decrypt(v) : defValue;
        }
    
        @Override
        public boolean contains(String s) {
            return delegate.contains(s);
        }
    
        @Override
        public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
            delegate.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
        }
    
        @Override
        public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
            delegate.unregisterOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
        }
    
    
    
    
        protected String encrypt( String value ) {
    
            try {
                final byte[] bytes = value!=null ? value.getBytes(UTF8) : new byte[0];
                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
                SecretKey key = keyFactory.generateSecret(new PBEKeySpec(SEKRIT));
                Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
                pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(Settings.Secure.getString(context.getContentResolver(),Settings.Secure.ANDROID_ID).getBytes(UTF8), 20));
                return new String(Base64.encode(pbeCipher.doFinal(bytes), Base64.NO_WRAP),UTF8);
    
            } catch( Exception e ) {
                throw new RuntimeException(e);
            }
    
        }
    
        protected String decrypt(String value){
            try {
                final byte[] bytes = value!=null ? Base64.decode(value,Base64.DEFAULT) : new byte[0];
                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
                SecretKey key = keyFactory.generateSecret(new PBEKeySpec(SEKRIT));
                Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
                pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(Settings.Secure.getString(context.getContentResolver(),Settings.Secure.ANDROID_ID).getBytes(UTF8), 20));
                return new String(pbeCipher.doFinal(bytes),UTF8);
    
            } catch( Exception e) {
                throw new RuntimeException(e);
            }
        }
    
    }
    
        3
  •  28
  •   Jeremy Logan    10 年前

    在Android活动中存储单个偏好的最简单方法是执行如下操作:

    Editor e = this.getPreferences(Context.MODE_PRIVATE).edit();
    e.putString("password", mPassword);
    e.commit();
    

    如果您担心这些密码的安全性,那么您可以在存储密码之前对其进行加密。

        4
  •  9
  •   Praful Bhatnagar    9 年前

    使用Richard提供的代码片段,您可以在保存密码之前对其进行加密。然而,首选项API并不能提供一种简单的方法来截取值并对其进行加密—您可以通过onPreferenceChangeListener阻止它被保存,理论上您可以通过PreferenceChangeListener修改它,但这会导致一个无止境的循环。

    我之前曾建议添加一个“隐藏”偏好,以实现这一点。这绝对不是最好的方法。我将提出另外两个我认为更可行的选择。

    首先,最简单的方法是在PreferenceChangeListener中,您可以获取输入的值,对其进行加密,然后将其保存到另一个首选项文件:

      public boolean onPreferenceChange(Preference preference, Object newValue) {
          // get our "secure" shared preferences file.
          SharedPreferences secure = context.getSharedPreferences(
             "SECURE",
             Context.MODE_PRIVATE
          );
          String encryptedText = null;
          // encrypt and set the preference.
          try {
             encryptedText = SimpleCrypto.encrypt(Preferences.SEED,(String)newValue);
    
             Editor editor = secure.getEditor();
             editor.putString("encryptedPassword",encryptedText);
             editor.commit();
          }
          catch (Exception e) {
             e.printStackTrace();
          }
          // always return false.
          return false; 
       }
    

    第二种方法,也是我现在更喜欢的方法,是创建自己的自定义首选项,扩展EditTextPreference,@override'ing setText() getText() 方法,以便 设置文本() 加密密码,以及 获取文本() 返回NULL。

        5
  •  5
  •   Jon O    12 年前

    我知道这有点死灵术,但你应该用安卓 AccountManager . 它是专门为这个场景构建的。这有点麻烦,但它所做的一件事是,如果SIM卡发生变化,本地凭证将失效,因此,如果有人刷卡并在手机中插入新的SIM卡,您的凭证将不会受到影响。

    这也为用户提供了一种快速简便的方法,从一个位置访问(并可能删除)他们在设备上拥有的任何帐户的存储凭据。

    SampleSyncAdapter 是一个使用存储帐户凭据的示例。

        6
  •  5
  •   Joe Plante    11 年前

    好吧,已经有一段时间了,答案是混合的,但这里有几个常见的答案。我研究得很疯狂,很难找到一个好答案

    1. 如果您假设用户没有根设备,那么模式私有方法通常被认为是安全的。您的数据以纯文本形式存储在文件系统的一部分中,该部分只能由原始程序访问。这使得在根设备上用另一个应用程序获取密码变得容易。同样,您想支持根设备吗?

    2. AES仍然是最好的加密方法。如果您正在启动一个新的实现,请记住查找这个,如果我发布这个实现已经有一段时间了。最大的问题是“如何处理加密密钥?”

    所以,现在我们在“怎么用钥匙?”部分。这是最困难的部分。拿到钥匙并没有那么糟糕。您可以使用密钥派生函数获取一些密码,并使其成为非常安全的密钥。你会遇到这样的问题:“你用pkfdf2能通过多少次?”但这是另一个话题

    1. 理想情况下,您将AES密钥存储在设备外。但是,您必须找到一种安全、可靠和安全地从服务器检索密钥的好方法。

    2. 您有某种类型的登录序列(甚至是远程访问的原始登录序列)。您可以在同一个密码上运行两次密钥生成器。它的工作原理是使用新的salt和新的安全初始化向量两次派生密钥。您将这些生成的密码之一存储在设备上,并使用第二个密码作为AES密钥。

    当您登录时,您将在本地登录上重新派生密钥,并将其与存储的密钥进行比较。完成后,您将为AES使用派生键2。

    1. 使用“一般安全”方法,您可以使用AES加密数据,并将密钥存储在模式\u private中。这是一个最近的ISH Android博客文章推荐的。不是非常安全,但对于某些人来说,它比纯文本更安全。

    你可以做很多变化。例如,您可以执行一个快速的pin(派生),而不是完整的登录序列。快速PIN可能不像完整的登录序列那样安全,但它比纯文本安全得多。

        7
  •  5
  •   dcgregorya    10 年前

    我会把我的帽子扔到戒指里,只是为了讨论安卓系统上的密码安全问题。在Android上,设备二进制文件应该被认为是泄露的——这对于直接由用户控制的任何终端应用程序都是一样的。从概念上讲,黑客可以使用对二进制文件的必要访问来对其进行反编译,并根除加密密码等。

    因此,如果安全问题是你的主要问题,我有两个建议要放弃:

    1)不要存储实际密码。存储授权的访问令牌,并使用访问令牌和电话签名对会话服务器端进行身份验证。这样做的好处是,您可以使令牌具有有限的持续时间,您不会破坏原始密码,并且您有一个好的签名,您可以在以后将其与流量关联(例如,检查入侵尝试并使令牌失效,使其无效)。

    2)采用2因素认证。这可能更烦人,更具侵入性,但对于某些合规情况,这是不可避免的。

        8
  •  2
  •   Marcell    11 年前

    您还可以查看这个小库,其中包含您提到的功能。

    https://github.com/kovmarci86/android-secure-preferences

    它类似于这里的其他围裙。希望有帮助:

        9
  •  2
  •   Suragch Shmidt    7 年前

    这是基于问题标题(和我一样)到达这里的人的补充答案,不需要处理与保存密码相关的安全问题。

    如何使用共享首选项

    用户设置通常保存在Android本地,使用 SharedPreferences 具有键值对。你使用 String 键保存或查找关联值。

    写入共享首选项

    String key = "myInt";
    int valueToSave = 10;
    
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putInt(key, valueToSave).commit();
    

    使用 apply() 而不是 commit() 在后台保存而不是立即保存。

    从共享首选项读取

    String key = "myInt";
    int defaultValue = 0;
    
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
    int savedValue = sharedPref.getInt(key, defaultValue);
    

    如果找不到键,则使用默认值。

    笔记

    • 与其像上面那样在多个地方使用本地键字符串,不如在单个位置使用常量。您可以在设置活动的顶部使用类似的内容:

      final static String PREF_MY_INT_KEY = "myInt";
      
    • 我用过 int 在我的示例中,但是您也可以使用 putString() , putBoolean() , getString() , getBoolean() 等。

    • documentation 了解更多详细信息。
    • 有多种方法可以获得共享的引用。见 this answer 寻找需要注意的东西。
        10
  •  1
  •   RenniePet    11 年前

    这个答案是基于马克建议的方法。将创建EditTextPreference类的自定义版本,该版本在视图中看到的纯文本和存储在首选项存储中的密码加密版本之间来回转换。

    正如大多数在这个线程上回答的人所指出的,这不是一种非常安全的技术,尽管安全程度部分取决于所使用的加密/解密代码。但它相当简单和方便,将阻止最随便的窥探。

    以下是自定义EditTextPreference类的代码:

    package com.Merlinia.OutBack_Client;
    
    import android.content.Context;
    import android.preference.EditTextPreference;
    import android.util.AttributeSet;
    import android.util.Base64;
    
    import com.Merlinia.MEncryption_Main.MEncryptionUserPassword;
    
    
    /**
     * This class extends the EditTextPreference view, providing encryption and decryption services for
     * OutBack user passwords. The passwords in the preferences store are first encrypted using the
     * MEncryption classes and then converted to string using Base64 since the preferences store can not
     * store byte arrays.
     *
     * This is largely copied from this article, except for the encryption/decryption parts:
     * https://groups.google.com/forum/#!topic/android-developers/pMYNEVXMa6M
     */
    public class EditPasswordPreference  extends EditTextPreference {
    
        // Constructor - needed despite what compiler says, otherwise app crashes
        public EditPasswordPreference(Context context) {
            super(context);
        }
    
    
        // Constructor - needed despite what compiler says, otherwise app crashes
        public EditPasswordPreference(Context context, AttributeSet attributeSet) {
            super(context, attributeSet);
        }
    
    
        // Constructor - needed despite what compiler says, otherwise app crashes
        public EditPasswordPreference(Context context, AttributeSet attributeSet, int defaultStyle) {
            super(context, attributeSet, defaultStyle);
        }
    
    
        /**
         * Override the method that gets a preference from the preferences storage, for display by the
         * EditText view. This gets the base64 password, converts it to a byte array, and then decrypts
         * it so it can be displayed in plain text.
         * @return  OutBack user password in plain text
         */
        @Override
        public String getText() {
            String decryptedPassword;
    
            try {
                decryptedPassword = MEncryptionUserPassword.aesDecrypt(
                         Base64.decode(getSharedPreferences().getString(getKey(), ""), Base64.DEFAULT));
            } catch (Exception e) {
                e.printStackTrace();
                decryptedPassword = "";
            }
    
            return decryptedPassword;
        }
    
    
        /**
         * Override the method that gets a text string from the EditText view and stores the value in
         * the preferences storage. This encrypts the password into a byte array and then encodes that
         * in base64 format.
         * @param passwordText  OutBack user password in plain text
         */
        @Override
        public void setText(String passwordText) {
            byte[] encryptedPassword;
    
            try {
                encryptedPassword = MEncryptionUserPassword.aesEncrypt(passwordText);
            } catch (Exception e) {
                e.printStackTrace();
                encryptedPassword = new byte[0];
            }
    
            getSharedPreferences().edit().putString(getKey(),
                                              Base64.encodeToString(encryptedPassword, Base64.DEFAULT))
                    .commit();
        }
    
    
        @Override
        protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
            if (restoreValue)
                getEditText().setText(getText());
            else
                super.onSetInitialValue(restoreValue, defaultValue);
        }
    }
    

    这显示了如何使用它-这是驱动首选项显示的“项目”文件。注意,它包含三个普通的edittextpreference视图和一个自定义的editpasswordpreference视图。

    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
        <EditTextPreference
            android:key="@string/useraccountname_key"
            android:title="@string/useraccountname_title"
            android:summary="@string/useraccountname_summary"
            android:defaultValue="@string/useraccountname_default"
            />
    
        <com.Merlinia.OutBack_Client.EditPasswordPreference
            android:key="@string/useraccountpassword_key"
            android:title="@string/useraccountpassword_title"
            android:summary="@string/useraccountpassword_summary"
            android:defaultValue="@string/useraccountpassword_default"
            />
    
        <EditTextPreference
            android:key="@string/outbackserverip_key"
            android:title="@string/outbackserverip_title"
            android:summary="@string/outbackserverip_summary"
            android:defaultValue="@string/outbackserverip_default"
            />
    
        <EditTextPreference
            android:key="@string/outbackserverport_key"
            android:title="@string/outbackserverport_title"
            android:summary="@string/outbackserverport_summary"
            android:defaultValue="@string/outbackserverport_default"
            />
    
    </PreferenceScreen>
    

    至于实际的加密/解密,这留给读者作为练习。我目前正在使用一些基于本文的代码 http://zenu.wordpress.com/2011/09/21/aes-128bit-cross-platform-java-and-c-encryption-compatibility/ 尽管键和初始化向量的值不同。

        11
  •  1
  •   Yauraw Gadav    11 年前

    首先,我认为用户的数据不应该存储在手机上,如果必须将数据存储在手机上的某个地方,应该在应用程序的私有数据中对其进行加密。用户凭证的安全性应该是应用程序的优先级。

    敏感数据应安全存储或根本不存储。在设备丢失或恶意软件感染的情况下,不安全存储的数据可能会受到损害。

        12
  •  0
  •   Braveblacklion    6 年前

    我使用android密钥库在ecb模式下使用rsa加密密码,然后将其保存在sharedreferences中。

    当我想要回密码时,我从共享的引用中读取加密的密码,然后使用密钥库对其进行解密。

    使用此方法,您将生成一个公钥/私钥对,其中私钥由Android安全存储和管理。

    以下是有关如何执行此操作的链接: Android KeyStore Tutorial

        13
  •  -2
  •   Artem Russakovskii    15 年前

    您需要使用sqlite、security apit来存储密码。 下面是存储密码的最佳示例--passwordsafe。 这是来源和解释的链接-- http://code.google.com/p/android-passwordsafe/

        14
  •  -2
  •   Rohit Jain    11 年前

    共享首选项是存储应用程序数据的最简单方法。但有可能任何人都可以通过应用程序管理器清除我们共享的首选项数据,所以我认为这对我们的应用程序来说并不完全安全。