【问题标题】:How can I protect "SecretStrings" so that they are not human readable in the compiled EXE file如何保护“SecretStrings”,使它们在编译的 EXE 文件中不可读
【发布时间】:2022-07-19 17:05:07
【问题描述】:

当我在 Visual Studio (C#) 中编写代码时,字符串会在 EXE 文件中编译。
但是如果我用文本编辑器打开 EXE 文件,我可以找到那些字符串。

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

我可以做些什么来保护这些字符串,以便它们以不可读的格式存储在 EXE 文件中?

如果可能,我想将字符串保留在我的 C# 代码中,而不是将它们保存在外部文件中。

【问题讨论】:

    标签: c# security encryption compilation data-protection


    【解决方案1】:

    我使用简单的异或加密。该程序仍然可读性很好。

    string sMyUser = XorEncrypt("bgnjm", 3); // "admin";
    string sMyPass = XorEncrypt("MlalgzPklvogHmltWkjp", 3); // "NobodyShouldKnowThis";
    Login(sMyUser, sMyPass);
    
    static private 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;
    }
    

    在这里您可以加密/解密您自己的字符串:
    https://dotnetfiddle.net/raoUBi

    【讨论】:

    • 只要你认识到这实际上并没有提供任何保护,我想只是混淆文件中的值是可以的。
    猜你喜欢
    • 2015-02-09
    • 1970-01-01
    • 2011-01-16
    • 2012-07-27
    • 2022-11-30
    • 2011-09-11
    • 2020-05-05
    • 1970-01-01
    • 2012-03-26
    相关资源
    最近更新 更多