比较两个字符串时如何将一个值替换为另一个值?

发布于 2024-07-13 21:31:31 字数 675 浏览 7 评论 0原文

我的用户在数据库中保存了一个密码字段,例如“0!ZWQ2”。 我必须将我的用户输入密码从“aA1234”解读为“0!ZWQ2”,并将其与数据库中的数据进行比较。

我比较的两个字符串是:

“abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890” 和 “9setybcpqwiuvxr108daj5'-`~!@#$%^&*()_+|}][{:.?/<>,;ZWQ2@#34KDA”

这样 a = 9 且 0 = A

如何我用 C# 做这个,有什么想法吗?

我只需要从用户输入中获取密码字段,并将其打乱为有趣的文本,以便将其与数据库中的数据进行比较。

任何帮助将不胜感激。

问候 艾蒂安

更新:(2009 年 2 月 10 日) 感谢大家的回复。 请注意,我确实意识到有更好的方法来处理这个问题。 但请注意,我正在创建一个 ASP.NET 应用程序,该应用程序将位于 SharePoint 中,连接到 Cobol 平面文件数据,而不是正确的数据库。 使用 Transoft 将我的 ASP.NET(ODBC 连接器)连接到 Cobol 平面文件。 所以我必须坚持使用这段代码,并且这不会在我的私人网站上使用。 我也无法控制何时在 Cobol 中创建密码。

My user have a password field such as “0!ZWQ2” saved in the database.
I must unscramble my User input password from “aA1234” to “0!ZWQ2” and compare it to data in a database.

The 2 strings that I compare is:

“abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ”
With
“9setybcpqwiuvxr108daj5'-`~!@#$%^&*()_+|}][{:.?/<>,;ZWQ2@#34KDA”

This way a = 9 and 0 = A

How would I do this in C#, any ideas?

I just need to take the password field from the user input and scramble it to funny text to compare it to the data in the Database.

Any help will be appreciated.

Regards
Etienne

Updates: (10 Feb 2009)
Thanks everyone for the replies. Please note that i do realize that there are much better ways of handeling this. But please note that I am creating an ASP.NET application thats goign to be sitting inside SharePoint connecting to Cobol flat file data and not a proper database. Using Transoft to connect my ASP.NET (ODBC connector) to the Cobol flat files. So i have to stick with this code, and this will not be used on my Private site. I also have no control on when the Password is created in Cobol.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

三寸金莲 2024-07-20 21:31:31

以下是要转换的快速、快速且较少的代码示例。

C# 代码:

        char[] OriginalChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
        char[] ScrambleChars = "9setybcpqwiuvxr108daj5'-`~!@#$%^&()+|}][{:.?/<>,;ZWQ2@#34KDART".ToCharArray();
        string TextToTransfer = "Hello";
        string NewText = "";
        foreach (char c in TextToTransfer)
        {
            NewText = NewText + ScrambleChars[Array.IndexOf<char>(OriginalChars, c)].ToString();
        }
        Console.WriteLine(NewText);

VB 代码:

    Dim OriginalChars() As Char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
    Dim ScrambleChars() As Char = "9setybcpqwiuvxr108daj5'-`~!@#$%^&()+|}][{:.?/<>,;ZWQ2@#34KDART"
    Dim TextToTransfer As String = "Hello"
    Dim NewText As String = ""
    For Each c As Char In TextToTransfer
        NewText += ScrambleChars(Array.IndexOf(OriginalChars, c))
    Next
    MsgBox(NewText)

Following is Quick, Fast, and lesser code example to convert.

Code in C#:

        char[] OriginalChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
        char[] ScrambleChars = "9setybcpqwiuvxr108daj5'-`~!@#$%^&()+|}][{:.?/<>,;ZWQ2@#34KDART".ToCharArray();
        string TextToTransfer = "Hello";
        string NewText = "";
        foreach (char c in TextToTransfer)
        {
            NewText = NewText + ScrambleChars[Array.IndexOf<char>(OriginalChars, c)].ToString();
        }
        Console.WriteLine(NewText);

Code in VB:

    Dim OriginalChars() As Char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
    Dim ScrambleChars() As Char = "9setybcpqwiuvxr108daj5'-`~!@#$%^&()+|}][{:.?/<>,;ZWQ2@#34KDART"
    Dim TextToTransfer As String = "Hello"
    Dim NewText As String = ""
    For Each c As Char In TextToTransfer
        NewText += ScrambleChars(Array.IndexOf(OriginalChars, c))
    Next
    MsgBox(NewText)
爱本泡沫多脆弱 2024-07-20 21:31:31

有什么特殊原因不使用标准哈希+盐来存储密码,而不是凯撒密码?

应该解决它的一种方法(未经测试的代码):

new string("aA1234".ToCharArray().Select(c => ScrambleChars[OriginalChars.IndexOf(c)]).ToArray());

Any special reason not to use a standard hash + salt for storing the passwords, instead of a Caesars cipher?

One way that should solve it (untested code):

new string("aA1234".ToCharArray().Select(c => ScrambleChars[OriginalChars.IndexOf(c)]).ToArray());
茶色山野 2024-07-20 21:31:31

您不应以可逆的方式将密码保存在数据库中。 数据库管理员或入侵您系统的黑客不应该能够获取用户存储的密码。

You should not save the passwords in your database in a way that can be reversed. The database administrator or a hacker intruding your system should not be able to get the user's stored passwords.

饭团 2024-07-20 21:31:31

首先,正如其他用户所说,如果您正在设计这个系统和密码存储方法,那么这种方法几乎毫无价值,应该使用许多更安全的方法。

然而,您要求这样做进行比较的事实让我认为您无法控制数据库中已有的内容,因此正在处理遗留的内容。

下面的函数会将您的用户密码作为字符串,并以字符串形式返回数据库密码。 需要注意的一件事是,您提供的两个字符串的长度不相等,因此字符 9 和 0 会崩溃。我已将其他字母 B 和 C 添加到 dbChars 字符串中。 另外,我“假设”输入的任何在映射字符中找不到的字符都只是按输入时附加。 您可能要求忽略这些字符。

    private string DatabasePassword(string userPassword)
    {
        //Constants showing mapping between user password and database password
        const string userChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        const string dbChars   = "9setybcpqwiuvxr108daj5'-`~!@#$%^&()+|}][{:.?/<>,;ZWQ2@#34KDABC";

        //Stringbuilder used to build the database password for output
        System.Text.StringBuilder databasePassword = new System.Text.StringBuilder();

        //Run through every character in the user password
        foreach (Char userChar in userPassword)
        {
            //Find the index of the user character in the userChars string.
            int userCharIndex = userChars.IndexOf(userChar);

            //if the userChar exists in the userChars string then map the character to the
            //equivalent database pasword character else just use the entered char
            char mappedChar = userCharIndex >= 0 ? dbChars[userChars.IndexOf(userChar)] : userChar;

            //Append mapped password to the output database password
            databasePassword.Append(mappedChar);
        }

        return databasePassword.ToString();
    }

Firstly, as other users have stated, if you are designing this system and password storage method then this method is almost worthless and there are many more secure methods that should be used instead.

However, the fact that you're asking for this to do comparison makes me think you do not have control over what is already in the database and therefore are working with something legacy.

The function below will take your users password as a string and return the database password as a string. One thing to note, the two strings you provided are not of equal length and therefore would crash for characters 9 and 0. I have added to additional letters B and C to the dbChars string. Also, I have "assumed" that any characters entered that cannot be found in the mapping characters are just appended as entered. You may require that these characters are ignored.

    private string DatabasePassword(string userPassword)
    {
        //Constants showing mapping between user password and database password
        const string userChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        const string dbChars   = "9setybcpqwiuvxr108daj5'-`~!@#$%^&()+|}][{:.?/<>,;ZWQ2@#34KDABC";

        //Stringbuilder used to build the database password for output
        System.Text.StringBuilder databasePassword = new System.Text.StringBuilder();

        //Run through every character in the user password
        foreach (Char userChar in userPassword)
        {
            //Find the index of the user character in the userChars string.
            int userCharIndex = userChars.IndexOf(userChar);

            //if the userChar exists in the userChars string then map the character to the
            //equivalent database pasword character else just use the entered char
            char mappedChar = userCharIndex >= 0 ? dbChars[userChars.IndexOf(userChar)] : userChar;

            //Append mapped password to the output database password
            databasePassword.Append(mappedChar);
        }

        return databasePassword.ToString();
    }
删除→记忆 2024-07-20 21:31:31

我会编写一个包含 Encode 或 Translate 方法的实用程序类,该方法接受 inputString 并(检查任何错误/恶意输入)使用您的密码方法对其进行变形。

return String.Compare(dbPassword, PasswordUtility.Encode(inputString));

I'd write a Utility-class that contains an Encode or Translate method which takes the inputString and (checks for any bad/malicious input) transfigures it using your cipher-method.

return String.Compare(dbPassword, PasswordUtility.Encode(inputString));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文