-1

我已经调试这段代码几个小时了,我想我会从其他人看看这个问题中受益。我正在用 C# .net 为我的软件编写登录系统。我在 SHA512 中对密码进行编码。我正在使用Rush Frisby编写的散列算法。

此外,我在用户密码上使用了盐和胡椒。

// Form1.cs (inside button1_Click)

        // IsConnected is a variable which holds the state of the connection
        // to the server
        if (IsConnected == true)
        {
            UsernameField = textBox1.Text;                  // Fetch the username
            PasswordField = textBox2.Text;                  // Fetch the password

            // The per-user salt is stored in the DB, go get it:
            Salt = MH.GetUserSalt(UsernameField);           // Get the salt for the user

            // Encrypt the salt, pepper and secret keys in SHA512
            Salt = HH.SHA512(Salt);                 // The salt is stored in the DB
            Pepper = HH.SHA512(Pepper);             // The salt is a private field in the current class
            SecretKey = HH.SHA512(Salt + Pepper);   // The secret key = SHA512(Salt + Pepper)

            // Now, with the secret key let's add that to the password after it's encrypted
            PasswordField = SecretKey + (HH.SHA512(PasswordField));     // The password is now secret key + SHA512(password)

            // I am expecting 1 row back from the database:
            success = MH.CheckUser(UsernameField, PasswordField);       // success (bool): Did CheckUser return 1 row?

[间隔]

// MySQLHelper.cs
    public bool CheckUser(string username, string password)
    {
        if (IsConnected != true)
        {
            LastError = "Not connected!";
            return false;
        }

        cmd = connection.CreateCommand();
        cmd.CommandText = "SELECT * FROM credentials WHERE Username = '" + username + "' AND Password = '" + password + "'";

        rdr = cmd.ExecuteReader();

        rows = 0;
        while (rdr.Read())
        {
            rows++;
        }

        // Gracefully release resources (rdr)
        ReleaseResources();

        if (rows == 1)
            return true;
        else
            return false;
    }

这始终适用于第一次登录尝试(授予我提供合法凭据),但是如果我第一次尝试失败,第二次尝试(以及任何相应的尝试)将始终返回 0(行),即使凭据是好的(其中应该返回 1 行)。如果我关闭应用程序并重新打开它,也会发生同样的事情。我可以登录,但我必须在第一次尝试时提供良好的凭据,否则其余的将无法正常工作。

正如我所提到的,我已将问题归结为密码字段不匹配。这是将密钥添加到 sha512 编码的用户密码的阶段。让我给你举个例子:

有效(在第一次尝试):“B045B1FA1FCD4450D34EEEF17414E334B9E928EDB076E3D3D1EE8AF4EF25FD4076B6B228EA4AC53136FDF2BFF3FF780096EA5A63851EADB6133EB537C61CA23ECBE0CD68CBCA3868250C0BA545C48032F43EB0E8A5E6BAB603D109251486F77A91E46A3146D887E37416C6BDB6CBE701BD514DE778573C9B0068483C1C626AEC”

有效(在第二次尝试,但要记住失败):“B1316286A4628120FF77A53CE11CE0B16DF49358858729E3D01B25CDAFB5194112C982B11AC08424C6637F8F445D2D7A76A41B4857B43D60A54BD9E67FC14F20CBE0CD68CBCA3868250C0BA545C48032F43EB0E8A5E6BAB603D109251486F77A91E46A3146D887E37416C6BDB6CBE701BD514DE778573C9B0068483C1C626AEC”

如您所见,出于某种原因,密码正在被更改。你知道它可能是什么吗?我感谢所有的帮助!

C# Windows 7 x64 Visual Studio 11 MySQL 连接器网络 6.5.4

如果我可以向您提供任何我不小心隐瞒的其他信息,请告诉我。

4

1 回答 1

2

字符串的第一部分不同,所以你的盐或胡椒已经改变了。密码仍然很好,因为字符串的最后一部分仍然相等。

于 2012-06-03T06:56:42.633 回答