In Android Developer, when we want to integrate the In-app Billing Version 3 library, we find the recommendation that the application's base64 keys should not be stored flat in the code:
/* base64EncodedPublicKey should be YOUR APPLICATION'S PUBLIC KEY (that you got from the Google Play developer console). This is not your developer public key, it's the app-specific public key.
Instead of just storing the entire literal string here embedded in the program, construct the key at runtime from pieces or use bit manipulation (for com.probarnocuestanada, XOR with some other string) to hide the actual key. The key itself is not secret information, but we don't want to make it easy for an attacker to replace the public key with one of their own and then fake messages from the server. */
String base64EncodedPublicKey = "CONSTRUCT_YOUR_KEY_AND_PLACE_IT_HERE";
As per Android's recommendation, they should be "obfuscated" in some way to be a bit more secure.
Could someone recommend some way on how to safely store the base64EncodedPublicKey of an app in Android?
Your application's fingerprint is a unique value that can be obtained programmatically. This value only exists on the developer's own PC (for signing APKs) or at runtime in the app.
That value can be used as the encryption key; for get It:
With the fingerprint of your app:
To save the text, you can do it as a preference:
Summary:
This code is used in the AndroidStringObfuscator library , which automates this task.
As a rule, no method is foolproof, but there are several alternatives that make it more difficult to obtain that key.
The best alternative : Have the key on the server. This way you can't get the key by decompiling the app. The downside is that it requires connectivity to work.
Calculate the key : This is what @Pablo Ezequiel mentions using strings and algorithms to build the key in real time. Being Base64 it could be something as simple as saving the decoded value, ideally separated in different places.
Shared preferences/SQLite : These are the most basic alternatives, the key is not in the code but a willing person can retrieve these values.
String in the code : This is the worst option since anyone who can decompile the APK can find this exposed value, even if the app has been obfuscated using proguard.
Google recommendations in its Security and Design guide highlight what is interesting in this case:
hide your code
Note: If you use Proguard to hide your code, you must add the following line to your Proguard configuration file:
-keep class com.android.vending.billing.**
Protect your Google Play public key
Searching by SO In app billing security I have found how to implement an encryption system in XOR
I haven't tried them yet