【问题标题】:Asp.net password encryption and decryption with saltasp.net密码加解密加盐
【发布时间】:2016-04-09 22:20:12
【问题描述】:

我正在尝试从表中提取加密密码,但收到此错误:

System.Data.SqlClient.SqlException:将 nvarchar 值 '84p37U29dna08XhUdV+bhQ==' 转换为数据类型 int 时转换失败。

值是我的加密密码。我不明白为什么会出现这个错误。代码在以下行中断:dataAdapter.Fill(dataSet);

protected bool attemptLogin(String username, String password)
    {
        bool validLogin = false;

        String sql = "SELECT Users.username, Passwords.password " +
                     "FROM Users INNER JOIN Passwords ON Users.userID = Passwords.password " +
                     "WHERE Users.username = @username";
        SqlCommand command = new SqlCommand(sql, dbConnection);
        command.Parameters.AddWithValue("@username", username);

        SqlDataAdapter dataAdapter = new SqlDataAdapter(command);

        // Set of Tables (in this instance, just one)
        DataSet dataSet = new DataSet();
        dataAdapter.Fill(dataSet);
        // First Table in DataSet
        DataTable table = dataSet.Tables[0];

        // Get value of password (should be in encrypted form)
        String dbPassword = table.Rows[0]["password"].ToString();

        // Decrypt password
        String decryptedPassword = decryptText("", dbPassword);
        if (decryptedPassword.Equals(password))
        {
            validLogin = true;
        }

        return validLogin;
    }

【问题讨论】:

  • 忽略你得到的错误,你的技术从根本上是有缺陷的。你不要解密密码!您存储一个加盐哈希,添加盐并散列给定密码并与存储的值进行比较。
  • @MitchWheat 但是,您不需要存储盐渍哈希。我使用 Buffer.BlockCopy 和 BitConverter 的组合,以及安全的哈希/盐生成器,只存储密码的哈希,实际上是哈希的哈希。
  • @cFrozenDeath 一旦所有信息都存储在一个字段中,您就很容易再次受到盐渍旨在解决的攻击(即彩虹表)。
  • @cFrozenDeath:不正确。我希望你没有像你描述的那样实施。请参阅 Rob 的评论。
  • 致 Rob 和 @MitchWheat,我想看看人们对此有何看法,所以我创建了这个:codereview.stackexchange.com/questions/115953/…

标签: c# sql asp.net sql-server encryption


【解决方案1】:

INNER JOIN Passwords ON Users.userID = Passwords.password

您正在尝试将用户 ID (int) 加入密码 (nvarchar)。

【讨论】:

    猜你喜欢
    • 2013-09-22
    • 2016-07-08
    • 2014-01-24
    • 2015-07-19
    • 1970-01-01
    • 2017-10-23
    • 2013-09-11
    • 2012-07-05
    • 2011-08-24
    相关资源
    最近更新 更多