Skip to content

Obfuscate C# source ​

Requires Codefend 3.3.0 or later for the recommended m_ preset.

Codefend can rename selected identifiers in C# source before compilation. This example calculates an order total, keeping Program, Main, and Console.WriteLine unchanged. It transforms source text; it is not a .NET assembly obfuscator.

1. Create the source ​

Install Node.js and a .NET 10 SDK. Install Codefend once on your development computer:

sh
npm install --global codefend

Create source/Program.cs:

csharp
using System;

public class Program
{
    private static int m_total(int m_unitPrice, int m_count, int m_discount)
    {
        return m_unitPrice * m_count - m_discount;
    }

    public static void Main()
    {
        Console.WriteLine(m_total(20, 4, 5));
    }
}

Expected obfuscated source ​

With the configuration in the next step, Codefend produces:

csharp
using System;

public class Program
{
    private static int Ox0(int Ox1, int Ox2, int Ox3)
    {
        return Ox1 * Ox2 - Ox3;
    }

    public static void Main()
    {
        Console.WriteLine(Ox0(20, 4, 5));
    }
}

The program's expected output remains 75. Generated identifier numbers can change when the source or Codefend version changes.

Only the private helper and its parameters use m_. Preserve public entry points and library APIs. This standalone .NET 10 example needs no .csproj, package.json, or npm project initialization.

2. Configure Codefend ​

Create .codefendrc.json alongside the source directory:

json
{
  "generation": {
    "inputDir": "source",
    "outputDir": "release"
  },
  "parser": {
    "name": "startWithMAndUnderscore"
  }
}

The startWithMAndUnderscore preset selects this example's m_ names, leaving unrelated underscore names alone. See parser presets for the exact rule and why we recommend this convention.

Plain Program.cs does not match the rule, so its filename stays unchanged. The default empty pool uses generated Ox names.

Keep source text-only and reserve sibling release for generated files: Codefend removes an existing output directory. See output-directory guidance.

3. Run both source versions ​

Run from the project root:

sh
dotnet run --file source/Program.cs
codefend --obfuscate
dotnet run --file release/Program.cs

Both programs should print:

text
75

The .NET 10 SDK's file-based app support compiles and runs each file directly, keeping build outputs in its temporary cache by default. release/Program.cs contains the generated helper and parameter names. Exact numbering is an implementation detail.

Applying this to a larger C# application ​

For an existing .csproj application, keep its normal project and build workflow. This single-file example avoids project setup; it does not replace the configuration required by a larger application.

Keep names used by reflection, serializers, model binding, dependency injection, public APIs, P/Invoke, and framework conventions stable unless all callers are intentionally updated. A private declaration alone does not prove that a name is safe to change.

Codefend matches text, including comments and string contents; it does not understand C# symbols or resolve reflection calls. Compile and test the generated application. Source obfuscation does not secure embedded credentials or transform existing DLLs. Review current limitations before processing multiple source files or adding replacement pools.

Add Codefend to your workflow ​

Place Codefend before compilation in your build script or CI job. The single-file example can use the command below. For an existing solution, keep the normal project configuration and arrange for its build and tests to consume the generated source rather than the original files.

sh
codefend --obfuscate
dotnet run --file release/Program.cs

In CI, stop on a failed step and package only validated generated output. See build and deployment for the shared workflow.