Java源码示例:com.google.android.vending.licensing.AESObfuscator
示例1
@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) {
}
}
}
示例2
@Before
public void initFixture() {
final byte[] SALT = new byte[] {
104, -12, 112, 82, -85, -10, -11, 61, 15, 54, 44, -66, -117, -89, -64, 110, -53, 123, 33
};
// Prepare PreferenceObfuscator instance
Context ctx = InstrumentationRegistry.getTargetContext();
sp = ctx.getSharedPreferences(filename, Context.MODE_PRIVATE);
String deviceId = Settings.Secure.getString(
ctx.getContentResolver(),
Settings.Secure.ANDROID_ID);
Obfuscator o = new AESObfuscator(SALT, ctx.getPackageName(), deviceId);
op = new PreferenceObfuscator(sp, o);
// Populate with test data
op.putString("testString", "Hello world");
op.commit();
}
示例3
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
mStatusText = (TextView) findViewById(R.id.status_text);
mCheckLicenseButton = (Button) findViewById(R.id.check_license_button);
mCheckLicenseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
doCheck();
}
});
mHandler = new Handler();
// Try to use more data here. ANDROID_ID is a single point of attack.
String deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
// Library calls this when it's done.
mLicenseCheckerCallback = new MyLicenseCheckerCallback();
// Construct the LicenseChecker with a policy.
mChecker = new LicenseChecker(
this, new ServerManagedPolicy(this,
new AESObfuscator(SALT, getPackageName(), deviceId)),
BASE64_PUBLIC_KEY);
doCheck();
}
示例4
@Before
public void initFixture() {
final byte[] SALT = new byte[] {
104, -12, 112, 82, -85, -10, -11, 61, 15, 54, 44, -66, -117, -89, -64, 110, -53, 123, 33
};
Context ctx = InstrumentationRegistry.getTargetContext();
String deviceId = Settings.Secure.getString(
ctx.getContentResolver(),
Settings.Secure.ANDROID_ID);
p = new APKExpansionPolicy(ctx,
new AESObfuscator(SALT, ctx.getPackageName(), deviceId));
}
示例5
@Before
public void initFixture() {
final byte[] SALT = new byte[] {
104, -12, 112, 82, -85, -10, -11, 61, 15, 54, 44, -66, -117, -89, -64, 110, -53, 123, 33
};
Context ctx = InstrumentationRegistry.getTargetContext();
String deviceId = Settings.Secure.getString(
ctx.getApplicationContext().getContentResolver(),
Settings.Secure.ANDROID_ID);
p = new ServerManagedPolicy(ctx.getApplicationContext(),
new AESObfuscator(SALT, ctx.getPackageName(), deviceId));
}
示例6
@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) {
}
}
示例7
@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) {
}
}
示例8
@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) {
}
}
示例9
@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) {
}
}
示例10
@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"));
}
示例11
public static Policy createPolicy(@NonNull Context context, @NonNull String packageName) {
String deviceID = Installation.id(context);
return new ServerManagedPolicy(context, new AESObfuscator(Key.SALT, packageName, deviceID));
}
示例12
@Before
public void setUp() throws Exception {
mObfuscator = new AESObfuscator(SALT, PACKAGE, DEVICE);
}