【发布时间】:2020-03-05 10:50:48
【问题描述】:
我正在尝试加密和解密存储在 SQL 中的密码。解码时出现错误输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符或填充字符中的非法字符。在 System.Convert.FromBase64_Decode。
解密代码:
string userEmail = Email1.Text;
string userPass = passW.Text;
SqlConnection sqlcon = new SqlConnection("My connection")
string query = "Select * from users Where email= '" + userEmail + "'";
SqlDataAdapter sda = new SqlDataAdapter(query, sqlcon);
DataTable dtbl = new DataTable()
sda.Fill(dtbl);
if (dtbl.Rows.Count == 1)
{
string savedPasswordHash = dtbl.Rows[0][1].ToString();
savedPasswordHash.Replace("-", "");
byte[] hashBytes = Convert.FromBase64String(savedPasswordHash);
byte[] salt = new byte[16];
Array.Copy(hashBytes, 0, salt, 0, 16);
var pbkdf2 = new Rfc2898DeriveBytes(userPass, salt, 10000);
byte[] hash = pbkdf2.GetBytes(20);
int ok = 1;
for (int i = 0; i < 20; i++)
if (hashBytes[i + 16] != hash[i])
ok = 0;
if (ok == 1) //good creds & redirect
加密代码:
byte[] salt1;
new RNGCryptoServiceProvider().GetBytes(salt1 = new byte[16]);
var pbkdf21 = new Rfc2898DeriveBytes(EmailTextBox.Text, salt1, 10000);
byte[] hash1 = pbkdf21.GetBytes(20);
byte[] hashBytes1 = new byte[36];
Array.Copy(salt1, 0, hashBytes1, 0, 16);
Array.Copy(hash1, 0, hashBytes1, 16, 20);
string savedPasswordHash1 = Convert.ToBase64String(hashBytes1);
string commString = $"UPDATE users SET NewPassword = ('{savedPasswordHash1}') where Email = ('{email2}')";
using (SqlConnection connect = new SqlConnection(constring))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = connect;
comm.CommandText = commString;
connect.Open();
comm.ExecuteNonQuery();
connect.Close();
}
列的数据类型是nvarchar
【问题讨论】:
-
请同时显示插入部分的代码。还要检查并给出数据库中密码字段的数据类型。您可能想要更多地隔离您的代码,因此请查看
savedPasswordHash中的实际内容 -
这看起来不太对...
Select后面不应该有一个列列表吗?string query = "Select from users Where email= '" + userEmail + "'"; -
OT 请使用参数而不是将值构建到 SQL 字符串中
-
@Emad 我在 savedPasswordHash 字符串中得到 7408207-16186516-9228325
-
SavedPasswordHash.Replace 没有任何作用