【问题标题】:How to compare a password against a hashed password with Scrypt.NET?如何使用 Scrypt.NET 将密码与散列密码进行比较?
【发布时间】:2016-09-19 07:32:34
【问题描述】:

注册后,我试图在 asp.net 中使用 scrypt 对来自用户的密码进行哈希处理,在数据库中,但是当我尝试登录时,我不知道如何将用户的密码与来自数据库的哈希。

谁能帮我弄清楚如何将密码与散列密码进行比较?

对于我使用的注册:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Security.Cryptography;
using Scrypt;

namespace WebApplication1
{
    public partial class SignUp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


        }



        protected void btSignup_Click(object sender, EventArgs e)
        {
            if (tbUname.Text != "" & tbPass.Text != "" && tbName.Text != "" && tbEmail.Text != "" && tbCPass.Text != "")
            {
                if (tbPass.Text == tbCPass.Text)
                {
                    String CS = ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString1"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(CS))
                    {
                        ScryptEncoder encoder = new ScryptEncoder();
                        string hashsedPassword = encoder.Encode(tbPass.Text);
                        SqlCommand cmd = new SqlCommand("insert into Users values('" + tbUname.Text + "','" + hashsedPassword + "','" + tbEmail.Text + "','" + tbName.Text + "')", con);
                        con.Open();
                        cmd.ExecuteNonQuery();

                        lblMsg.Text = "Registration Succesfull";
                        lblMsg.ForeColor = Color.Green;
                        Response.Redirect("~/SignIn.aspx");
                    }
                }
                else { lblMsg.Text = "Passwords do not match"; }
            }

            else
            {
                lblMsg.ForeColor = Color.Red;
                lblMsg.Text = "All Fields are Mandatory";

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            SqlConnection con1 = new SqlConnection();
            con1.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Integrated Security=True";
            con1.Open();
            SqlCommand cm1 = new SqlCommand();
            cm1.CommandText = "select * from [Users]where Username=@Uname";
            cm1.Parameters.AddWithValue("@Uname", tbUname.Text);
            cm1.Connection = con1;
            SqlDataReader rd = cm1.ExecuteReader();
            if (rd.HasRows)
            {
                Label1.Visible = true;
                Label1.Text = "Username already exists !";
                Label1.ForeColor = System.Drawing.Color.Red;
            }

            else
            {
                Label1.Visible = true;
                Label1.Text = "Username is available !";
                Label1.ForeColor = System.Drawing.Color.Green;
            }
        }
    }
}

然后登录:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace WebApplication1
{
    public partial class SignIn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            String CS = ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString1"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS)) {
                SqlCommand cmd= new SqlCommand("select * from Users where Username='"+ Username.Text+"' and Password='"+Password.Text+"'" , con);
                con.Open();
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                if (dt.Rows.Count != 0)
                {
                    Session["USERNAME "] = Username.Text;
                    Response.Redirect("~/UserHome.aspx"); }
                else {
                    lblError.Text = "Invalid Username or Password !";

                }
            }
        }
    }
}

【问题讨论】:

  • 我在 VS2012 中使用 Scrypt.NET 包,其功能:为密码生成新哈希:ScryptEncoder encoder = new ScryptEncoder();字符串 hashsedPassword = encoder.Encode("mypassword");将密码与散列密码进行比较: ScryptEncoder encoder = new ScryptEncoder(); bool areEquals = encoder.Compare("mypassword", hashedPassword);但我不知道如何在我的代码中,在 SIGN-IN 中将部分与“比较”集成。
  • 没关系。那么,您能帮我解决我的问题吗?

标签: asp.net hash cryptography passwords scrypt


【解决方案1】:

Scrypt.NET 为您处理输入的密码和现有哈希的比较。文档页面显示:

ScryptEncoder encoder = new ScryptEncoder();

bool areEquals = encoder.Compare("mypassword", hashedPassword);

在您的情况下,这意味着您不能在 SQL 查询中使用密码来获取特定用户。您只需使用给定的Username 即可在Users 表中找到正确的行。

SqlCommand cmd = new SqlCommand("select * from Users where Username=@Username" , con);
cmd.Parameters.Add("@Username", SqlDbType.NVarChar, 255, Username.Text);

con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count != 0) {
    ScryptEncoder encoder = new ScryptEncoder();

    foreach(DataRow row in dt.Rows)
    {
        if (encoder.Compare(Password.Text, (string)row["Password"]))
        {
            Session["USERNAME "] = Username.Text;
            Response.Redirect("~/UserHome.aspx");
            return;
        }
    }
} else {
    lblError.Text = "Invalid Username or Password !";
}

始终使用参数化的 SQL 查询。否则,您很容易受到 SQL 注入攻击。

【讨论】:

  • 我不知道这段访问 DataTable 的代码是否正确。如果您发现错误,请更正。
  • 它在“if (encoder.Compare(Password.Text, row["Password"]))”处显示 2 个错误:1. 'Scrypt.ScryptEncoder.Compare( string, string)' 有一些无效的参数。论点 2:无法从 'object' 转换为 'string'
  • 在这里转换成字符串就足够了。
  • 如果这个答案解决了这个问题,你可以accept这个答案。
猜你喜欢
  • 1970-01-01
  • 2020-09-21
  • 1970-01-01
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
  • 2018-09-17
  • 1970-01-01
相关资源
最近更新 更多