2012-03-13 46 views
2

感謝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(); 
     } 

    } 
} 

回答

0

首先,我知道這是比較方便的Preference意見,但在你的特殊情況下,我寧願採取一個簡單的EditTex t並在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並查看這種加密是如何工作的!

+0

很酷,我會看看日誌。我不想繼續輸入密碼,我想至少它不會是明文。 – Mike 2012-03-13 01:52:49

+1

您可以在用戶第一次運行應用程序時生成一個隨機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

+0

@haibison鹽不會真的改變任何東西,因爲用戶有權訪問它(至少如果電話是根植的)!但這不是一個「我該如何執行安全性」的問題。 – 2012-03-14 01:35:55

-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(); 
     } 
    } 
}