【问题标题】:extending the base EditTextPreference and encrypting/decrypting扩展基本 EditTextPreference 和加密/解密
【发布时间】:2012-03-29 10:33:30
【问题描述】:

感谢 dmon 和此处的示例 Process the value of preference before save in Android?

我能够获取基本代码。但是我的值没有被加密存储在设备上的 preferences.xml 中,我知道这是我的一个简单错误(java 新手)。

我的加密和解密类在 EditTextPreference 代码之外工作。

亲切的问候,

迈克

我的preferences.xml

    <ping.test.com.EncryptedEditTextpreference 
        android:key="key" 
        android:summary="Enter Your Public Key" 
        android:title="Public Key" 
        android:inputType="textPassword"/>

</PreferenceCategory>

我的类扩展 EditTextPreference

package ping.test.com;

import android.content.Context;
import android.preference.EditTextPreference;
import android.util.AttributeSet;

public class EncryptedEditTextPreference extends EditTextPreference {
  public EncryptedEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  public EncryptedEditTextPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public EncryptedEditTextPreference(Context context) {
    super(context);
  }

  @Override
  public String getText() {
    String value = super.getText();
    try {
        return SimpleCrypto.decrypt("BiteMe", value);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return value;
  }

  @Override
  protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
    super.setText(restoreValue ? getPersistedString(null) : (String) defaultValue);
  }

  @Override
  public void setText(String text) {

        try {
            super.setText(SimpleCrypto.encrypt("BiteMe", text ));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

  }
}

【问题讨论】:

  • 我正在尝试在类中扩展 EditTextPreference,但出现以下错误。 'android.preference.EditTextPreference' 中没有可用的默认构造函数任何想法如何解决这个问题?

标签: android encryption passwords preferences


【解决方案1】:

在显示设置时不显示加密值,因为我屏蔽了 pswd。我只想在存储时加密

protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
    if (!restoreValue) {
        super.setText((String) defaultValue);
    }
    else {
        try {
            String decrypted = SimpleCrypto.decrypt(Constants.MasterKey, getPersistedString(null));
            super.setText(decrypted);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

【讨论】:

    【解决方案2】:

    首先,我知道拥有Preference 视图很方便,但在您的特殊情况下,我更愿意采用简单的 EditText 并在SharedPreferences 中手动保存首选项。

    回答您的问题:根据文档,它应该可以工作,您尝试了什么。为了更接近错误尝试添加这样的日志:

    @Override
    public void setText(String text) {
        Log.v("setText", "from " + text);
        try {
            String to = SimpleCrypto.encrypt("BiteMe", text );
            Log.v("setText", "to " + to);
            super.setText(to);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    验证,您的 SimpleCrypt 类按预期工作,您可以将 TextWatcher 添加到 EditText 并记录它以查看会发生什么。

    请注意,这可能根本不安全,因为攻击者能够反编译您的 apk 并查看此加密是如何工作的!

    【讨论】:

    • 酷,我会看看日志。我知道对设备的物理访问是一个问题,但不确定对于用户不打算使用的应用程序还能做什么想连续输入密码,我猜至少不会是明文。
    • 您可以在用户第一次运行应用程序时生成随机盐来加密数据:java.util.UUDI.randomUUID().toString()。为了更安全,您可以使用lock pattern,快速简单:-) 为了确保用户无法清除数据(偏好中有您的盐),请在标签application(AndroidManifest.xml)中使用此标志:@987654329 @.
    • @haibison 盐并没有真正改变任何东西,因为用户可以访问它(至少在手机被植根的情况下)!但这不是“如何实现安全”的问题。
    • @Rafael,谢谢,我误解了盐的概念。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多