String Encryption replaces the strings in the constant pools of your processed classes with encrypted values. Your running app decrypts them when it needs them. This makes string constants harder to read through static analysis of your code.
String Encryption protects sensitive strings and makes your software harder to understand. For example, an attacker can't easily search for the text of a specific message to find where your code uses it.
This article explains how String Encryption works. To configure it, see Configuring String Encryption.
Use Removal With String Encryption
String Encryption alone protects most strings, but not always inlined string constants. The Java compiler inlines string constants, copying them into every place they're used. DashO doesn't encrypt the original field values, because that could cause incorrect behavior. For example, it could break the value of a library API constant that an external application uses.
Some string constants remain unencrypted after String Encryption. To protect them, configure DashO to remove unused members. DashO doesn't count inlined references as uses of the defining field, so Removal removes those fields. For more information, see Removal.
Remove these constants only if external applications don't use them. Choose the Removal setting for your project type:
| Project type | Removal setting | Effect |
| Library | Remove if not public | DashO doesn't remove strings with public or protected modifiers. Don't make strings that need protection public. |
| All other projects | Remove | DashO removes unused fields. |
To fine-tune these settings, exclude classes, methods, or fields from Removal.
Because of inlining, String Encryption must protect both the class that defines a string constant and every class that references it. Otherwise, some copies of the string stay unencrypted. Keep this in mind if you exclude classes or methods from String Encryption for size or performance reasons.
String Encryption and Reflection in Android Mode
In Android Mode, R8 handles Renaming and Removal, and DashO handles String Encryption. R8 detects some classes accessed through reflection and excludes them from Renaming and Removal on its own.
However, String Encryption can stop R8 from recognizing class names in String literals. R8 may then rename or remove classes your app loads through reflection, which causes runtime errors. To prevent these errors, do one of the following:
- Add R8
-keeprules so R8 doesn't rename or remove the classes your app loads through reflection. - Exclude the methods that contain reflection calls from String Encryption, so R8 detects the classes they load. To let R8 rename those classes and update the String literals to match, also use
-adaptclassstrings.
String Encryption Level
The String Encryption Level, from 1 to 10, controls the strength and performance of the encryption and decryption algorithms. A higher level encrypts more strongly, but takes longer to decrypt at runtime and may slow down your app. The default is 2.
Number of Decrypters
You set how many decrypters DashO generates and adds to the classes in each output. The default is 2. DashO chooses decrypter method names and signatures randomly, except when you use an input map file.
Where DashO places decrypters, and how many it creates, depends on your configuration:
| Configuration | Decrypter placement |
| Default | DashO adds the configured number of decrypters to each output, internal to the JAR files that use them. |
| Unmerged outputs | DashO multiplies the configured number of decrypters by the number of inputs. |
User Property INJECT_DECRYPTERS_GLOBALLY set to true
|
DashO places decrypters globally and divides the configured number among the outputs, instead of adding it to each output. |
| Android Mode | DashO always places decrypters globally. |
Intern Strings
Intern Strings controls whether the injected decrypter calls intern() on strings before it returns them. The intern() method makes sure equal strings exist only once in memory.
For more information, see Class String.
Decrypter Classes
You can control to which classes may serve as outer classes for the generated anonymous static inner classes that will house the decrypters. You can specify the following criteria for these classes:
- Packages the classes must not come from
- Modifiers the classes must match, such as
public - A regular expression, glob pattern, or name the class name must match
By default, DashO excludes all java., javax., and android. packages, and only includes public classes.
String Encryption Map Files
If you use Incremental Obfuscation, DashO must reproduce the decrypters from your initial obfuscation. Otherwise, newly obfuscated code may not work with previously obfuscated code. A String Encryption Map File records which decrypters DashO placed in a previous run, and where.
| Map file | What DashO does |
| Output map file | Writes information about the decrypters it produces in the build. |
| Input map file | Reproduces the decrypters described in the file. DashO ignores the String Encryption Level and Number of Decrypters settings. |
For more information, see Incremental Obfuscation.
Includes and Excludes
String Encryption adds size and runtime cost, so you may want to apply it only to the more sensitive parts of your app. DashO decides which methods to protect as follows:
| Rules defined | Methods DashO protects |
| No include rules | All methods not covered by an exclude rule. |
| One or more include rules | Only included methods. |
Custom Encryption
Use your own encryption and decryption methods in place of DashO's default String Encryption. For more information, see Custom Encryption.
Limitations and Troubleshooting
| Limitation or issue | Details |
| A string constant appears unencrypted in the protected code. | The compiler inlined it. Enable Removal, and protect both the defining class and every referencing class. |
| A string reused across methods isn't encrypted. | Method-level include or exclude rules can cause this. Review the rules for methods that share the string. |
| Classes loaded through reflection fail at runtime in Android Mode. | String Encryption stopped R8 from recognizing class names. |
String comparisons return false for equal strings. |
Intern Strings is off and the code uses ==. Turn on Intern Strings, or use .equals(). |
| Newly obfuscated code doesn't work with previously obfuscated code. | Use a String Encryption input map file from the earlier build. Not available in Android Mode. |
| The app is slower or larger. | String Encryption has size and performance costs. Lower the level, or exclude less sensitive code. |
Related Articles