A wrapper around SharedPreferences that encrypts / decrypts all shared preferences.

Warning - do not assume use of this class will prevent access to shared preferences. If an attacker has enough access to acquire your password store, they have enough access to acquire your source binary and figure out your encryption key.

However, this class will prevent casual investigators from acquiring passwords, and thereby may prevent undesired negative publicity.

Full disclosure: This code was adapted from Michael Burton's response on StackOverflow

To use you must extend the CryptoSharedPreferences abstract class and implement the getSpecialCode() method. Ideally this method would use either user input or an external source to get the password used to encrypt the shared preferences. The demo has the value hard-coded and is significantly less secure as a result.

The demo EncryptedPreferences object is shown below.

public class EncryptedPreferences extends CryptoSharedPreferences {

    public EncryptedPreferences(Context context, SharedPreferences delegate) {
        super(context, delegate);

     * This should be replaced with a user input pass phrase or an externally
     * retrieved pass phrase if possible.
     * @return
    protected char[] getSpecialCode() {
        return "THIS IS MY ENCRYPTING KEY PHRASE@@!".toCharArray();

    public Set<String> getStringSet(String s, Set<String> strings) {
        return null;

The activity using the EncryptedPreferences object.

public class ObscuredPrefsActivity extends OakDemoActivity {

    private static final String MY_APP_PREFENCES_NAME = "my_app_prefences_name";

    private SharedPreferences mNormalSharedPreferences;
    private EncryptedPreferences mEncryptedPreferences;
    @InjectView( private EditText mEditText;
    @InjectView( private TextView mTextView;
    @InjectView( private TextView mDecryptedText;
    @InjectView( private TextView mDecryptedDesc;

    protected void onCreate(Bundle savedInstanceState) {

        mNormalSharedPreferences = getSharedPreferences(MY_APP_PREFENCES_NAME, MODE_PRIVATE);

        //EncryptedPreferences is defined by you and extends CryptoSharedPreferences
        mEncryptedPreferences = new EncryptedPreferences(this, mNormalSharedPreferences);

    public void saveEncryptedClicked(View view) {
        //save input as encrypted
                .putString("first_name", mEditText.getText().toString())


    public void saveNormallyClicked(View view) {
        //save unencrypted
                .putString("first_name", mEditText.getText().toString())


    private void showRawContents(boolean isEncrypted) {
        //demonstrate what could be viewed by a light-weight hacker
        mTextView.setText(mNormalSharedPreferences.getString("first_name", ""));

        //note: to read encrypted data, you would just use mEncryptedPreferences.getString(...
        if (isEncrypted) {
            mDecryptedText.setText(mEncryptedPreferences.getString("first_name", ""));
        } else {

Screenshot of demo activity.

Crypto Shared Preferences