Asp.net密码加密和解密用盐

时间:2016-01-05 22:54:55

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

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

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;
    }

1 个答案:

答案 0 :(得分:4)

{{1}}

您正在尝试将UserID(int)加入密码(nvarchar)。