# # Code Virtualization

The Common Intermediate Language (CIL) is a set of platform-independent instructions generated by the language-specific compiler (C#, VB.NET...) from the source code. The CIL is platform-independent and can be executed on any Common Language Infrastructure supported environments such as the .NET runtime or Mono.

The way how the CIL and other metadata is stored must follow a specific standard (ECMA-335). This way tools like .NET Reflector or ILSpy can read the CIL instructions and translate the code back to its source language (C#, VB.NET...).

Code Virtualization converts the CIL code into a set of randomized instructions which are interpreted at runtime by our virtual machine. As there is no standardized procedure to interpret the new instruction set correctly, the original CIL instructions can't be reconstructed. Consequently, the virtualized code can't be translated back to its source language.

As the performance of virtualized methods is significantly decreased, code virtualization should only be applied to selected methods.

To enable code virtualization you need to decorate the corresponding methods with the following attribute:

    [System.Reflection.ObfuscationAttribute(Feature = "Virtualization", Exclude = false)]

## # Example Usage

Before:

    [System.Reflection.Obfuscation(Feature = "Virtualization", Exclude = false)]
    public void CreateFile(string filename, string content)
    {
    	string directory = Path.GetDirectoryName(filename) ;
    	if (!Directory.Exists(directory))
    	{
    		Directory.CreateDirectory(directory);
    	}
    	StreamWriter streamWriter = new StreamWriter(File.Open(filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite));
    	streamWriter .Write(content);
    	streamWriter.Flush();
    	streamWriter.Close();
    }

After Protection (decompiled with ILSpy):

Code Virtualization After

The remaining stub code executes the virtual machine.