Obfuscate, a command to protect your code
If you want to protect your code, this command will help you to make it unreadable by renaming the member of a class to unmeaning names.
For example, given the following class:
public class Person
{
private int _id;
private string _name;
private string _lastName;
public Person()
{
}
public int Id
{
get { return _id; }
set { _id = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
}
Place the cursor inside the class and execute this command (Obfuscate – obf) and the class will be converted to:
public class Person
{
private int áàà;
private string âàà;
private string ãàà;
public Person()
{
}
public int äàà
{
get { return áàà; }
set { áàà = value; }
}
public string åàà
{
get { return âàà; }
set { âàà = value; }
}
public string æàà
{
get { return ãàà; }
set { ãàà = value; }
}
}This will result in an unreadable code for everyone, included you, so use it with special care, just before a release version and keeping a copy of the original source code.
Some notes about this command:
- The command will perform a refactor rename, so all references in the solution will also be renamed
- In a partial class, all parts of the class will be renamed
- If a class implement an interface, the IDE will prompt advising that the change may affect other classes
- In some complex cases, the rename could make the code to fail compiling, so try to compile the solution after using this command
- If you want to undo the class obfuscation, just use the regular Undo Edit menu item
How it Works
The following code shows the main methods in the Obfuscate command.
private void ObfuscateClass(CodeClass2 classElement)
{
foreach (CodeElement2 partialElement in classElement.Parts)
{
foreach (CodeElement2 codeElement in partialElement.Children)
{
if (codeElement.Kind == vsCMElement.vsCMElementClass)
{
ObfuscateClass(codeElement as CodeClass2);
}
else if (codeElement.Name != classElement.Name)
{
try
{
codeElement.RenameSymbol(GetObfusctatedName());
}
catch (Exception e) { MessageBox.Show(e.ToString()); }
}
}
}
}
private string GetObfusctatedName()
{
_nameIndex++:
return String.Format("{0}{1}{2}{3}",
_prefix,
Char.ConvertFromUtf32((_nameIndex & 0x0f) + 0xE0),
Char.ConvertFromUtf32(((_nameIndex >> 4) & 0x0f) + 0xE0),
Char.ConvertFromUtf32(((_nameIndex >> 8) & 0x0f) + 0xE0));
}
The method ObfuscateClass() enumerate all members of the current class and execute the IDE command RenameSymbol on it.
First, it enumerates the parts of a partial class:
foreach (CodeElement2 partialElement in classElement.Parts)
Then enumerate the members of the class:
foreach (CodeElement2 codeElement in partialElement.Children)
If the member name match the name of the class, assume it’s a constructor, so skip.
if (codeElement.Name != classElement.Name)
The GetObfuscatedName() function just build an unique name for the current member, using an UTF32 string.
Try it!
Download the Obfuscate command and try it now. To add this command to your set of commands copy the two files in the zip (Obfuscate.autox, Obfuscate.cs) into the “AutoCode 2010\Commands” folder. This folder is located in My Documents folder.
Comments
Re: Obfuscate, a command to protect your code
posted by Nickk on Thursday, August 18, 2011
There is a bug in the .zip command, missing "_nameIndex++" in GetObfuscatedName() function.
Re: Obfuscate, a command to protect your code
posted by devprojects on Thursday, August 18, 2011
Thanks Nick, Solved!
