Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a more secure way to store sensitive string in an Android project?

In my Android project, I want to keep my web service address unknown. I store the link as follows:

private final String SERVICE_LINK = "mywebservicelink..."

But since I saw that APK's can be decompiled, I wonder if this link can be known.

How can I store it securely?

Thanks.

like image 345
Kerem Avatar asked Oct 13 '15 08:10

Kerem


People also ask

How to secure sensitive data in Android?

To provide additional protection for sensitive data, you can encrypt local files using the Security library. This measure can provide protection for a lost device without file system encryption.

How to secure string in Android?

For String encryption we have created an gradle plugin to hide your keys inside your Android app using the NDK and XOR operator. The goal is to obfuscate your keys to prevent reverse engineering of your app. You can optionally provide a custom encoding/decoding algorithm to improve the security of your key.


2 Answers

You can set sensitive string in your gradle.properties

https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_properties_and_system_properties

For example:

in your gradle.properties (usually located in your root project), you can define:

SERVICE_LINK = "mywebservicelink"

Then in your application's build.gradle

android {
   ...
   defaultConfig {
       resValue "string", "service_link", SERVICE_LINK
   }
   ...
}

Then, this service link will be ready in your resource as R.string.service_link. That is, you can simply get the value by doing:

getString(R.string.service_link);

EDIT 1:

If you're asking about how to hide your strings in your APK, then you can use ProGuard. However, be advised. Whatever you put into your source code, there's no 100% guarantee that it cannot be reverse-engineered. ProGuard will obfuscate your code, which will make reverse-engineering harder significantly.

For more information, this thread is awesome: How to avoid reverse engineering of an APK file?

like image 52
Fadils Avatar answered Dec 08 '22 11:12

Fadils


You can assign value of SERVICE_LINK variable after doing some operations, instead of assigning directly. For example using string operations with a few meaningless strings, getting characters or substrings from some specific positions, getting some characters from ASCII code obtained by some arithmetic operations etc. may be helpful for the purpose.

This provides a complexity so that third party people don't find your constant value easily. They may think it's a dynamic value varying on run time, or something unrelated. But if it was in " " directly, it would be found easily.

like image 40
Safa Kadir Avatar answered Dec 08 '22 10:12

Safa Kadir