Overview
StringEncryptPass is an obfuscation pass that replaces plaintext StringLiteral nodes with calls to an injected decode helper, so strings are not visible in the compiled binary. This protects sensitive strings like API keys, URLs, and messages from being easily extracted.
The pass uses XOR encoding with a configurable key (default
0x5A). At runtime, the injected _obfDecode$ helper reverses this encoding to recover the original string.Class Definition
lib/src/engine/passes/string_encrypt/string_encrypt_pass.dart:14
Constructor Parameters
The XOR key used for encoding string literals. Must be a valid byte value (0-255).
How It Works
Encoding
Each UTF-8 byte of the string is XORed with the
xorKey to produce an encrypted byte arrayDecoding
At runtime, the injected
_obfDecode$ helper XORs the bytes again with the same key to recover the original stringInjected Helper Function
The pass automatically injects this helper into the first user library:lib/src/engine/passes/string_encrypt/string_encrypt_pass.dart:52
Transformation Example
Before:Configuration Options
TheStringEncryptPass behavior is controlled through PassOptions:
Regular expression patterns for string literals that should not be encrypted. Useful for preserving strings that must remain in plaintext (e.g., protocol identifiers, file extensions).
String Filtering
The pass automatically skips:- Constant expressions (strings inside
ConstantExpressionnodes) - Annotations (strings in class/method annotations)
- Dart SDK library strings (strings in
dart:*libraries) - Strings matching exclude patterns (configured via
stringExcludePatterns) - External package strings (strings not in the project package)
Methods
run()
- Finds the first user library to inject the
_obfDecode$helper - Injects the helper procedure
- Walks the component and replaces string literals with decode calls
The Dart kernel component to transform
Shared context containing options, symbol table, and name generator
encode()
xorKey.
The string to encode
The encoded byte array where each byte is
original_byte ^ xorKeylib/src/engine/passes/string_encrypt/string_encrypt_pass.dart:178
Usage Example
Basic Usage
With PassRunner
Combined with Other Passes
Encoding Example
Testing
test/engine/passes/string_encrypt_pass_test.dart:12
Performance Considerations
The XOR decoding operation is extremely fast (bitwise operations) and has negligible runtime overhead. The Dart VM can optimize these operations efficiently.
Security Notes
XOR encoding is not cryptographically secure. Itβs designed to prevent casual inspection of strings in the binary, not to protect against determined attackers. For highly sensitive data, consider using proper encryption libraries.
Related
PassRunner
Orchestrates multiple passes
RenamePass
Rename identifiers to meaningless names
DeadCodePass
Injects unreachable dead code branches
RefractorEngine
Main obfuscation engine