首页 > 解决方案 > “索引超出数组边界”错误背后的原因

问题描述

我编写了一个从数据库中检索散列密码并将其与登录表单中的密码进行比较的代码。

                if (usercheck.HasRows)
                {
                    while (usercheck.Read())
                    {
                        string hashst = usercheck.GetValue(1).ToString();

                        byte[] hashBytes = Convert.FromBase64String(hashst);

                        byte[] salt = new byte[16];
                        Array.Copy(hashBytes, 0, salt, 0, 16);

                        var pbkdf2 = new Rfc2898DeriveBytes(passsign.Text, salt, 100000);
                        byte[] hash = pbkdf2.GetBytes(20);

                        for (int i = 0; i < 20; i++)
                            if (hashBytes[i + 16] != hash[i])
                            {
                                lblUsPsav.Text = "Incorrect Password";
                            }
                            else
                            {
                                    Response.Redirect(String.Format("./Profile.aspx"));
                            }

但是,错误显示代码上的“索引超出了错误的范围”

for (int i = 0; i < 20; i++)

我不知道为什么。相反,关于数据检索和其他任何事情,代码都可以完美运行。

标签: c#mysqlasp.nethashwebforms

解决方案


推荐阅读