/** * Constructor. * * @param context the caller's context */ public SecurePreferencesOld(Context context) { // Proxy design pattern if (SecurePreferencesOld.sFile == null) { SecurePreferencesOld.sFile = PreferenceManager.getDefaultSharedPreferences(context); } // Initialize encryption/decryption key try { final String key = SecurePreferencesOld.generateAesKeyName(context); String value = SecurePreferencesOld.sFile.getString(key, null); if (value == null) { value = SecurePreferencesOld.generateAesKeyValue(); SecurePreferencesOld.sFile.edit().putString(key, value).commit(); } SecurePreferencesOld.sKey = SecurePreferencesOld.decode(value); } catch (Exception e) { if (sLoggingEnabled) { Log.e(TAG, "Error init:" + e.getMessage()); } throw new IllegalStateException(e); } // initialize OnSecurePreferencesChangeListener HashMap sOnSharedPreferenceChangeListeners = new HashMap<OnSharedPreferenceChangeListener, OnSharedPreferenceChangeListener>(10); }
@Deprecated private static String decrypt(String ciphertext) { if (ciphertext == null || ciphertext.length() == 0) { return ciphertext; } try { final Cipher cipher = Cipher.getInstance(AES_KEY_ALG, PROVIDER); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(SecurePreferencesOld.sKey, AES_KEY_ALG)); return new String(cipher.doFinal(SecurePreferencesOld.decode(ciphertext)), "UTF-8"); } catch (Exception e) { if (sLoggingEnabled) { Log.w(TAG, "decrypt", e); } return null; } }