public class SecurePreferences
extends java.lang.Object
SharedPreferences object
in android that encrypts the data and keys when saving content to the Preference store.| Modifier and Type | Class and Description |
|---|---|
static class |
SecurePreferences.SecurePreferencesException
Secure Preference Exception error for dealing with
errors in operation for this class
|
| Constructor and Description |
|---|
SecurePreferences(android.content.Context context,
java.lang.String preferenceName,
java.lang.String secureKey,
java.lang.String salt,
boolean encryptKeys)
This will initialize an instance of the SecurePreferences class
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Clear out all the key/value pairs from the store
|
boolean |
containsKey(java.lang.String key)
Determin if a key exists in the secure store
|
protected byte[] |
createKeyBytes(java.lang.String key)
Create the key bytes
|
protected java.lang.String |
decrypt(java.lang.String securedEncodedValue)
Decrypt a given encrypted value
|
protected java.lang.String |
encrypt(java.lang.String value,
javax.crypto.Cipher writer)
Encrypt a given string value
|
boolean |
getBoolean(java.lang.String key)
Get a boolean value from the encrypted store
|
protected javax.crypto.spec.IvParameterSpec |
getIv(java.lang.String salt)
Get the Iv Parameter Spec
|
protected javax.crypto.spec.SecretKeySpec |
getSecretKey(java.lang.String key)
Create the Secret Key Spec
|
java.lang.String |
getString(java.lang.String key)
Get a String value from the encrypted store.
|
java.util.Set<java.lang.String> |
getStringSet(java.lang.String key)
Get a String set from the secured preferences
|
protected void |
initCiphers(java.lang.String secureKey,
java.lang.String salt)
Initialize the Ciphers used in encryption/decryption
|
void |
put(java.lang.String key,
java.lang.String value)
Put a string into the secure preference store
|
void |
putApply(java.lang.String key,
java.lang.String value)
Put a string into the secure preference store using the
SharedPreferences.Editor.apply() method
instead of SharedPreferences.Editor.commit() |
void |
putBoolean(java.lang.String key,
boolean value)
Put a boolean value into the secure preference store
|
void |
putStringSet(java.lang.String key,
java.util.Set<java.lang.String> value)
Put a secure set of strings into the secure store
|
void |
removeValue(java.lang.String key)
Remove a key/value pair from the secure store
|
public SecurePreferences(android.content.Context context,
java.lang.String preferenceName,
java.lang.String secureKey,
java.lang.String salt,
boolean encryptKeys)
throws SecurePreferences.SecurePreferencesException
context - your current context.preferenceName - name of preferences file (preferenceName.xml)secureKey - the key used for encryption, finding a good key scheme is hard.
Hardcoding your key in the application is bad, but better than plaintext preferences. Having the user enter the key upon application launch is a safe(r) alternative, but annoying to the user.encryptKeys - settings this to false will only encrypt the values,
true will encrypt both values and keys. Keys can contain a lot of information about
the plaintext value of the value which can be used to decipher the value.SecurePreferences.SecurePreferencesExceptionprotected void initCiphers(java.lang.String secureKey,
java.lang.String salt)
throws java.io.UnsupportedEncodingException,
java.security.NoSuchAlgorithmException,
java.security.InvalidKeyException,
java.security.InvalidAlgorithmParameterException
secureKey - the encryption keyjava.io.UnsupportedEncodingExceptionjava.security.NoSuchAlgorithmExceptionjava.security.InvalidKeyExceptionjava.security.InvalidAlgorithmParameterExceptionprotected javax.crypto.spec.IvParameterSpec getIv(java.lang.String salt)
protected javax.crypto.spec.SecretKeySpec getSecretKey(java.lang.String key)
throws java.io.UnsupportedEncodingException,
java.security.NoSuchAlgorithmException
key - the encryption keyjava.io.UnsupportedEncodingExceptionjava.security.NoSuchAlgorithmExceptionprotected byte[] createKeyBytes(java.lang.String key)
throws java.io.UnsupportedEncodingException,
java.security.NoSuchAlgorithmException
key - the encryption keyjava.io.UnsupportedEncodingExceptionjava.security.NoSuchAlgorithmExceptionpublic void put(java.lang.String key,
java.lang.String value)
key - the key to store the data atvalue - the value to encrypt and storepublic void putApply(java.lang.String key,
java.lang.String value)
SharedPreferences.Editor.apply() method
instead of SharedPreferences.Editor.commit()key - the key of the value to sentvalue - the value to encrypt and storepublic void putBoolean(java.lang.String key,
boolean value)
key - the key of the value to sentvalue - the boolean value you want to encrypt and storepublic void putStringSet(java.lang.String key,
java.util.Set<java.lang.String> value)
key - the key valuevalue - the string set to encodepublic boolean containsKey(java.lang.String key)
key - the key to check forpublic void removeValue(java.lang.String key)
key - the key of the value you want to removepublic java.lang.String getString(java.lang.String key)
throws SecurePreferences.SecurePreferencesException
key - the key of the value you wish to receiveSecurePreferences.SecurePreferencesExceptionpublic boolean getBoolean(java.lang.String key)
throws SecurePreferences.SecurePreferencesException
key - the key of the value you wish to retrieveSecurePreferences.SecurePreferencesException - Such error. Much headache. Wow.public java.util.Set<java.lang.String> getStringSet(java.lang.String key)
throws SecurePreferences.SecurePreferencesException
key - the key of the setSecurePreferences.SecurePreferencesException - Such error. Much headache. Wow.public void clear()
protected java.lang.String encrypt(java.lang.String value,
javax.crypto.Cipher writer)
throws SecurePreferences.SecurePreferencesException
value - the value to encryptwriter - the encryption cipherSecurePreferences.SecurePreferencesExceptionprotected java.lang.String decrypt(java.lang.String securedEncodedValue)
securedEncodedValue - the encrypted value you wish to decrypt