0

I want to encrypt my WPF code so when I open the EXE file with softwares like 'ILSpy' or 'IL Disassembler' I couldn't see the code.

I don't want to use with any existing software but to write the encryption myself.

Who can give me a simple code which does it? (Do I need to use the 'System.Security.Cryptography' namespace?)

Thanks...

Benjy
  • 49
  • 6

3 Answers3

1

You can't really encrypt the code (there are several attempts - some of them like the infamous Themida actually work) but you can obfuscate it. There are many premade tools like Dotfuscator that are designed for this exact purpose.

If you, however, want to write your own obfuscator, you have to get to "low level", which in .NET context is CIL. You may rename all variables and classes, wrap common method calls to some ambiguous code and such but it will be hard work and eventually, if it is important to someone, it will be deobfuscated.

nxu
  • 2,202
  • 1
  • 22
  • 34
1

There is no simple solution to the problem you are talking about. That's why there several, sometime pretty expensive, commercial tools. They are called obfuscators. Since you apparently don't have an idea how obfuscation works and probably not even how CLR works (you would not have asked your question being it otherwise), it would be, most likely, a suicide mission to work on a custom obfuscator. Hence my recommendation is go and buy a license for a tool that will fit your requirements.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
0

I assume you want to store encrypted XAML in your project and then decrypt and compile it on the fly. First write a command-line utility to encrypt your files i.e. something like this:

private static byte[] Key = { 123, 217, 19, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 112, 222, 209, 241, 24, 175, 144, 173, 53, 196, 29, 24, 26, 17, 218, 131, 236, 53, 209 };
    private static byte[] Vector = { 146, 64, 191, 111, 23, 3, 113, 119, 231, 121, 252, 112, 79, 32, 114, 156 };

    static void Main(string[] args)
    {
        var xaml = File.ReadAllBytes(args[0]);
        using (var outputFile = new FileStream(args[1], FileMode.Create))
        {
            RijndaelManaged rm = new RijndaelManaged();
            var transform = rm.CreateEncryptor(Key, Vector);
            CryptoStream cs = new CryptoStream(outputFile, transform, CryptoStreamMode.Write);
            cs.Write(xaml, 0, xaml.Length);
            cs.FlushFinalBlock();
        }
    }

Now back in your main main application set your XAML Build Actions to "None" and add Pre-build commands in your project settings to encrypt each of your files (I'll pretend here you're just keeping them as encrypted files in the same folder as your exe, in reality you'd made them embedded resources or something). Then it's simply a matter of loading them, decrypting them and compiling them:

private void LoadEncryptedXaml(string filename)
    {
        RijndaelManaged rm = new RijndaelManaged();
        var transform = rm.CreateDecryptor(Key, Vector);

        var bytes = File.ReadAllBytes(filename);
        using (var stream = new MemoryStream(bytes))
        using (var cs = new CryptoStream(stream, transform, CryptoStreamMode.Read))
        {
            var decrypted = ReadFully(cs);
            using (var memstream = new MemoryStream(decrypted))
            {
                var xaml_object = XamlReader.Load(memstream);

                // do something with it here, e.g. if you know it's  a resource dictionary then merge it with the other app resources
                Application.Current.Resources.MergedDictionaries.Add(xaml_object as ResourceDictionary);
            }
        }
    }

The ReadFully function just reads until the end of the stream, you can get the source code here.

Community
  • 1
  • 1
Mark Feldman
  • 15,731
  • 3
  • 31
  • 58