C# 如何将此解密合并到我的代码中,将哈希密码翻译回原始密码

C# 如何将此解密合并到我的代码中,将哈希密码翻译回原始密码,c#,sql,asp.net,C#,Sql,Asp.net,我正在创建一个网站,其中有一个SQL数据库连接到网站的注册和登录功能。我希望在将密码发送到数据库之前对其进行散列,我已经成功地完成了这项工作,但是,当用户尝试登录时,我发现很难将散列后的密码解密回来 这是我的用户登录页面,我想在这里合并以下代码 using System.Collections.Generic; using System.Configuration; using System.Data.SqlClient; using System.Linq; using System.Web;

我正在创建一个网站,其中有一个SQL数据库连接到网站的注册和登录功能。我希望在将密码发送到数据库之前对其进行散列,我已经成功地完成了这项工作,但是,当用户尝试登录时,我发现很难将散列后的密码解密回来

这是我的用户登录页面,我想在这里合并以下代码

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

namespace DogWalkingSite
{
    public partial class userlogin : System.Web.UI.Page
    {
        string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(strcon);
                //checking to see if the connection is closed
                if (con.State == System.Data.ConnectionState.Closed)
                {
                    //opens state to connect to database
                    con.Open();
                }

                SqlCommand cmd = new SqlCommand("select * from user_master_tbl where user_id= '"+TextBox1.Text.Trim()+ "' and password= '" + TextBox2.Text.Trim() + "'", con);
                SqlDataReader dr = cmd.ExecuteReader();
                //HasRows will become false if the inputs are false
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        Response.Write("<script>" + "alert('"+dr.GetValue(5).ToString()+"');" + "</script>");
                        //sessions used to determine when to show buttons
                        Session["username"] = dr.GetValue(5).ToString();
                        Session["name"] = dr.GetValue(0).ToString();
                        Session["role"] = "user";
                    }

                    Response.Redirect("homepage.aspx");
                }
                else
                {
                    Response.Write("<script>" + "alert('Username does not exist');" + "</script>");
                }

            }
            catch (Exception ex)
            {

            }
        }
    }
} ```




``` public static bool VerifyPassword(string username,

                    string password,AccountDataContext context)

    {

        var user = context.UserAccounts.FirstOrDefault(p => p.UserName == username);

        if (user != null)

        {

            string salt = user.Password.Substring(user.Password.Length - DefaultSaltSize);

            string hashedPassword = CreateHash(password, salt);

            return hashedPassword.Equals(user.Password);

        }

        return false;



    } ```


使用System.Collections.Generic;
使用系统配置;
使用System.Data.SqlClient;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
命名空间DogWalkingSite
{
公共部分类userlogin:System.Web.UI.Page
{
字符串strcon=ConfigurationManager.ConnectionString[“con”].ConnectionString;
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
尝试
{
SqlConnection con=新的SqlConnection(strcon);
//检查连接是否已关闭
if(con.State==System.Data.ConnectionState.Closed)
{
//打开状态以连接到数据库
con.Open();
}
SqlCommand cmd=new SqlCommand(“从用户_-master_-tbl中选择*,其中用户_-id=”+TextBox1.Text.Trim()+“,密码=”+TextBox2.Text.Trim()+”,con);
SqlDataReader dr=cmd.ExecuteReader();
//如果输入为假,HasRows将变为假
如果(哈斯罗博士)
{
while(dr.Read())
{
Write(“+”警报(“+dr.GetValue(5.ToString()+”);“+”);
//用于确定何时显示按钮的会话
会话[“用户名”]=dr.GetValue(5).ToString();
会话[“名称”]=dr.GetValue(0).ToString();
会话[“角色”]=“用户”;
}
重定向(“homepage.aspx”);
}
其他的
{
Write(“+”警报('用户名不存在');“+”);
}
}
捕获(例外情况除外)
{
}
}
}
} ```
```公共静态布尔验证密码(字符串用户名,
字符串密码,AccountDataContext(上下文)
{
var user=context.UserAccounts.FirstOrDefault(p=>p.UserName==UserName);
如果(用户!=null)
{
字符串salt=user.Password.Substring(user.Password.Length-DefaultSaltSize);
字符串hashedPassword=CreateHash(密码,salt);
返回hashedPassword.Equals(user.Password);
}
返回false;
} ```

当用户注册网站时,对值进行散列并将其保存到数据库中,并将用户每次登录时的值与散列值进行比较

解密散列是不可能的。因为散列是单向函数。但您可以尝试使用字典攻击破解。()


嗯。。这有帮助吗?

你原来的问题已经得到了回答,但我想帮你解决

这是一个评论太多,所以我只会张贴在这里

而不是:

SqlCommand cmd = new SqlCommand("select * from user_master_tbl where user_id= '"+TextBox1.Text.Trim()+ "' and password= '" + TextBox2.Text.Trim() + "'", con);
这样做:

SqlCommand cmd = new SqlCommand("select * from user_master_tbl where user_id = '@user' and password = '@pass'", con);
cmd.Parameters.AddWithValue("@user", TextBox1.Text.Trim());
cmd.Parameters.AddWithValue("@pass", FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text.Trim(), "SHA1"));

总是像这样参数化用户输入。这将保护您免受SQL注入攻击。另外,我觉得它更具可读性。

只是想加入一些最近教给我的东西,一个真正的密码散列,是无法逆转的

这就是加密和密码散列的区别

虽然破解加密的东西需要一些努力,但这是可以做到的。 密码散列是一种方法

我有一个处理这两个问题的Nuget包,下面是一个实时示例:

DataJuggler.Core.Cryptography.Net Core

DataJuggler.Net.Cryptography.Net框架

  // Create passwordHash, trying up to 3 times to ensure it can be verified
  passwordHash = CryptographyHelper.GeneratePasswordHash(password, keyCode, 3);

  // Verify Hash
  verified = CryptographyHelper.VerifyHash(password, keyCode, passwordHash);
因此,在数据库中存储密码散列,如果被黑客攻击,值如下所示:

6ERTG31 W4WKD2EEKALHTUWZD6N2W8VNRHTN+tozftd+5ZIK2JXUYLL0MZJPR1J1XROBFMBBSGZAPEBY2LJ+lmoUu4tWpYvrBsFIpvmwb/cw=

现场演示:

视频,包括3D动画,以说明这一点:


“将散列密码翻译回原始密码”-您不需要。为了简单起见,您可以直接比较散列,而不是解密散列以比较原始值(这是不可能的..它是散列)。散列密码的整个要点是,您无法从散列中反转实际密码。是的,两个不同的密码可能会导致相同的散列。除了上面的注释之外,您需要存储散列的密码,也许还需要重新访问salt方法。salt应该是随机的。您应该在数据库中存储HASH(pwd),正如我从您的问题中了解到的那样,您已经这样做了。然后,您应该将行[“passwordColumn”]与HASH(suppliedPassword)进行比较,以检查输入的密码是否与注册期间(或上次密码更改期间)设置的密码匹配。这里的盐不能是随机的,因为通常盐是这样使用的:HASH(pwd+Salt)。如果使用随机salt,还应将其存储在数据库中,以便在比较期间能够使用相同的值进行salt,例如:If(HASH(HASH(suppliedPassword+row[“salt”])==row[“passwordColumn]){//GRANT ENTRY}谢谢,但是我想其他的评论让我意识到我是多么的愚蠢,因为我帮助了我,我真的很感激。我已经试着按照你说的做了,这看起来对吗?“”“SqlCommand cmd=new SqlCommand”(“select*from user_master_tbl其中user_id=”“+TextBox1.Text.Trim()+”和password=“+TextBox2.Text.Trim()+”,con);cmd.Parameters.AddWithValue(“”,TextBox1.Text.Trim());cmd.Parameters.AddWithValue(“”,FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox