如何保护“SecretStrings”,使其在编译后的 EXE 文件中不被人类读取

问题描述 投票:0回答:1

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

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

如何保护这些字符串,使其以不可读的格式存储在 EXE 文件中?

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

c# security encryption compilation data-protection
1个回答
1
投票

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

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;
}

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

© www.soinside.com 2019 - 2024. All rights reserved.