感謝DMON和這裏的例子Process the value of preference before save in Android?擴展基本EditTextPreference和加密/解密
我能得到基本的代碼了。但我的價值並沒有被存儲在設備上的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();
}
}
}
很酷,我會看看日誌。我不想繼續輸入密碼,我想至少它不會是明文。 – Mike 2012-03-13 01:52:49
您可以在用戶第一次運行應用程序時生成一個隨機salt來加密數據:java.util.UUDI.randomUUID()。toString()'。爲了更安全起見,您可以使用[鎖定模式](https://code.google.com/p/android-lockpattern/),快速簡單:-)確保用戶無法清除數據(其中含有首選項),在標籤'application'(AndroidManifest.xml):'android:manageSpaceActivity =「your-fake-activity」'中使用這個標誌。 – 2012-03-13 03:41:41
@haibison鹽不會真的改變任何東西,因爲用戶有權訪問它(至少如果電話是根植的)!但這不是一個「我該如何執行安全性」的問題。 – 2012-03-14 01:35:55