public void putString(String key, String value) { if (mEditor == null) { mEditor = mPreferences.edit(); } String obfuscatedValue = mObfuscator.obfuscate(value, key); mEditor.putString(key, obfuscatedValue); }
public String getString(String key, String defValue) { String result; String value = mPreferences.getString(key, null); if (value != null) { try { result = mObfuscator.unobfuscate(value, key); } catch (ValidationException e) { // Unable to unobfuscate, data corrupt or tampered Log.w(TAG, "Validation error while reading preference: " + key); result = defValue; } } else { // Preference not found result = defValue; } return result; }
private void isInvertible(String original) throws Exception { assertEquals(original, mObfuscator.unobfuscate(mObfuscator.obfuscate(original, original + "Key"), original + "Key")); } }
@Test public void obfuscateSame() throws Exception { String obfuscated = mObfuscator.obfuscate("test", "testKey"); assertEquals(obfuscated, mObfuscator.obfuscate("test", "testKey")); Obfuscator same = new AESObfuscator(SALT, PACKAGE, DEVICE); assertEquals(obfuscated, same.obfuscate("test", "testKey")); assertEquals("test", same.unobfuscate(obfuscated, "testKey")); }
@Test public void unobfuscateDifferentKey() throws Exception { String obfuscated = mObfuscator.obfuscate("test", "testKey"); Obfuscator differentPackage = new AESObfuscator(SALT, "package2", DEVICE); try { differentPackage.unobfuscate(obfuscated, "notMyKey"); fail("Should have thrown ValidationException"); } catch (ValidationException expected) { } }
public String getString(String key, String defValue) { String result; String value = mPreferences.getString(key, null); if (value != null) { try { result = mObfuscator.unobfuscate(value, key); } catch (ValidationException e) { // Unable to unobfuscate, data corrupt or tampered Log.w(TAG, "Validation error while reading preference: " + key); result = defValue; } } else { // Preference not found result = defValue; } return result; }
public void putString(String key, String value) { if (mEditor == null) { mEditor = mPreferences.edit(); } String obfuscatedValue = mObfuscator.obfuscate(value, key); mEditor.putString(key, obfuscatedValue); }
@Test public void unobfuscateDifferentSalt() throws Exception { String obfuscated = mObfuscator.obfuscate("test", "testKey"); Obfuscator differentSalt = new AESObfuscator(new byte[]{1}, PACKAGE, DEVICE); try { differentSalt.unobfuscate(obfuscated, "testKey"); fail("Should have thrown ValidationException"); } catch (ValidationException expected) { } }
public String getString(String key, String defValue) { String result; String value = mPreferences.getString(key, null); if (value != null) { try { result = mObfuscator.unobfuscate(value, key); } catch (ValidationException e) { // Unable to unobfuscate, data corrupt or tampered Log.w(TAG, "Validation error while reading preference: " + key); result = defValue; } } else { // Preference not found result = defValue; } return result; }
public void putString(String key, String value) { if (mEditor == null) { mEditor = mPreferences.edit(); } String obfuscatedValue = mObfuscator.obfuscate(value, key); mEditor.putString(key, obfuscatedValue); }
@Test public void unobfuscateDifferentPackage() throws Exception { String obfuscated = mObfuscator.obfuscate("test", "testKey"); Obfuscator differentPackage = new AESObfuscator(SALT, "package2", DEVICE); try { differentPackage.unobfuscate(obfuscated, "testKey"); fail("Should have thrown ValidationException"); } catch (ValidationException expected) { } }
public String getString(String key, String defValue) { String result; String value = mPreferences.getString(key, null); if (value != null) { try { result = mObfuscator.unobfuscate(value, key); } catch (ValidationException e) { // Unable to unobfuscate, data corrupt or tampered Log.w(TAG, "Validation error while reading preference: " + key); result = defValue; } } else { // Preference not found result = defValue; } return result; }
public void putString(String key, String value) { if (mEditor == null) { mEditor = mPreferences.edit(); } String obfuscatedValue = mObfuscator.obfuscate(value, key); mEditor.putString(key, obfuscatedValue); }
@Test public void unobfuscateDifferentDevice() throws Exception { String obfuscated = mObfuscator.obfuscate("test", "testKey"); Obfuscator differentDevice = new AESObfuscator(SALT, PACKAGE, "device2"); try { differentDevice.unobfuscate(obfuscated, "testKey"); fail("Should have thrown ValidationException"); } catch (ValidationException expected) { } }
public String getString(String key, String defValue) { String result; String value = mPreferences.getString(key, null); if (value != null) { try { result = mObfuscator.unobfuscate(value, key); } catch (ValidationException e) { // Unable to unobfuscate, data corrupt or tampered Log.w(TAG, "Validation error while reading preference: " + key); result = defValue; } } else { // Preference not found result = defValue; } return result; }
public void putString(String key, String value) { if (mEditor == null) { mEditor = mPreferences.edit(); } String obfuscatedValue = mObfuscator.obfuscate(value, key); mEditor.putString(key, obfuscatedValue); }
@Test public void unobfuscateAvoidBadPaddingException() throws Exception { // Length should be equal to the cipher block size, to make sure that all padding lengths // are accounted for. for (int length = 0; length < 255; length++) { char[] data = new char[length]; Arrays.fill(data, '0'); String input = String.valueOf(data); Log.d(TAG, "Input: (" + length + ")" + input); String obfuscated = mObfuscator.obfuscate(input, "testKey"); Obfuscator differentSalt = new AESObfuscator(new byte[]{1}, PACKAGE, DEVICE); try { differentSalt.unobfuscate(obfuscated, "testKey"); fail("Should have thrown ValidationException"); } catch (ValidationException expected) { } } }
@Test public void unobfuscateInvalid() throws Exception { try { mObfuscator.unobfuscate("invalid", "testKey"); fail("Should have thrown ValidationException"); } catch (ValidationException expected) { } }