Search code examples
c#securityencryptioncompilationdata-protection

How can I protect "SecretStrings" so that they are not human readable in the compiled EXE file


When I write code in Visual Studio (C#) then Strings are compiled inside the EXE file.
But if I open the EXE file with a text editor I can find those Strings.

    string sMyUser = "admin";
    string sMyPass = "NobodyShouldKnowThis";
    Login(sMyUser, sMyPass);

What can I do to protect these strings, so that they are stored in the EXE file in a unreadable format?

If possible I want to leave the strings inside my C# code and not save them in external files.


Solution

  • I use a simple XOR encryption. The program is still readable very well.

    string sMyUser = XorEncrypt("bgnjm", 3); // "admin";
    string sMyPass = XorEncrypt("MlalgzPklvogHmltWkjp", 3); // "NobodyShouldKnowThis";
    Login(sMyUser, sMyPass);
    
    private static string XorEncrypt(string text, int key)
    {
        string newText = "";
        for (int i = 0; i < text.Length; i++)
        {
            int charValue = Convert.ToInt32(text[i]); //get the ASCII value of the character
            charValue ^= key; //xor the value
            newText += char.ConvertFromUtf32(charValue); //convert back to string
        }
        return newText;
    }
    

    Here you can encrypt/decrypt your own string:
    https://dotnetfiddle.net/raoUBi