【发布时间】: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