Encoding strings
Published 31 December 2012
Managed software stores all the strings in one place and in a clear structure. This makes it easy to find the strings in a decompiled assembly. By following the references to these strings, it may be possible to understand the purpose of your code, even after obfuscation.
String encoding protects against this.
Using string encoding
To encode strings, in the Project Settings window, go to Strings Encoding or click the icon in the toolbar.
Select I want to hide important information by encoding all the strings in my software.
A const string
cannot be encoded. You should use static readonly string
instead.
Strings encoding options
The following strings encoding options are available:
I want to use strings encoding with improved protection | Protects against assembly modification. You cannot use this feature if you use another tool to modify your assembly after SmartAssembly. You can't use this option with assemblies using any version of Windows Phone 7.x XNA, Xbox XNA, or with SQL CLR assemblies. |
I want to compress and encrypt the encoded strings | Improves protection and reduces assembly size. You can't use this option with assemblies using Xbox XNA, or with SQL CLR assemblies. |
I want my application to cache strings when decoded | Enables strings to be loaded quickly. You can use this option with all of the technologies supported by SmartAssembly. |
The strings are automatically decoded, when needed, at runtime.
When string encoding is enabled, the colored bars under the toolbar icon and to the left of the feature options are green. If it is disabled, the bars are orange.
Turning off Strings Encoding for selected members
You can prevent selected strings from being encoded by applying a [
DoNotEncodeStrings]
attribute to any method, class, and struct.
You can also prevent strings from the entire assembly from being encoded, by applying this attribute for the whole assembly: [assembly: DoNotEncodeStrings]
(it's recommended you add it to AssemblyInfo.cs file for readability).
Adding a [
DoNotEncodeStrings]
attribute prevents strings from being encoded in the member the attribute is applied on, and all child members. For example, if you apply it to a class, strings in all methods will be excluded from strings encoding.
If you used the attribute to turn off Strings Encoding on any member, you can turn the strings encoding back on for selected child members by applying a [
EncodeStrings]
attribute.
// You need to turn on the Strings Encoding in the // Project Settings first for the attributes to take effect [assembly: DoNotEncodeStrings] // Turn off strings encoding for the entire assembly class ClassWithoutEncodedStrings { private void MethodWithoutEncodedStrings() { } [EncodeStrings] // Revert the [DoNotEncodeStrings] on the parent member (assembly) private void MethodWithEncodedStrings() { } }
String encoding examples
The examples below show the effect of encoding three strings within an assembly:
Before string encoding:
"Fred.gate@hotmail.com" "p@ssw0rd1" "licensingUrl=http://licensing.red-gate.com/licensingactivation.asmx"
After string encoding:
"FNRFk1TTJZM016VTPQ==.bmV1dSBzcGVjaWZpZWQ9sbGVjdGlvbi4=L"
Using strings encoding with enums
Note that enums are normally obfuscated, but you would not want an obfuscated enum value to be displayed in your UI. SmartAssembly assumes that enum values which have Format
, GetName
, GetNames
, Parse
, TryParse
, or ToString
called on them will be displayed to users, and so these enums are excluded from obfuscation automatically.
If your code uses enums in other ways, you can exclude all enums from obfuscation:
- Close SmartAssembly
- Use an XML editor to open the *.saproj file for your SmartAssembly project
- In the
<
Options>
section, add the following element:
<DoNotObfuscateAnyEnums/>
- Save the *.saproj file, and reopen the project in SmartAssembly.