Я использую Encrypt и Decrypt для имени пользователя / пароля и сохраняю его в базе данных, в то же время я использую его с типом пользователя, который может войти в систему с собственной панели.
При регистрации Encrypt работал нормально и сохранял в базе данных, но когда я пытался войти в систему, пароль, который я вводил, не работал. Я думаю, что расшифровка не работала, чтобы вызвать пароль, который я ввел, и я не могу войти в интерфейс.
Я использую System.Security.Cryptograph и System.IO.
Файл класса
class Cryptography
{
public static string Encrypt(string clearText)
{
string EncryptionKey = "dk&;=GZ>j6KSev,<dm>cZG's$maAiD";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
public static string Decrypt(string cipherText)
{
string EncryptionKey = "dk&;=GZ>j6KSev,<dm>cZG's$maAiD";
cipherText = cipherText.Replace(" ", "+");
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
}
Файл для входа
private void btnLogin_Click(object sender, EventArgs e)
{
Connection.Open();
MySqlDataAdapter sda = new MySqlDataAdapter("SELECT RegistrationType FROM registration Where Username='" + tbUsername.Text + "' and Password='" + tbPassword.Text + "'", Connection);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count == 1)
//if(dt.Rows[0][0].ToString() == "1")
{
switch (dt.Rows[0]["RegistrationType"] as string)
{
case "Administrator":
{
this.Hide();
Dashboard.dbAdmin DashboardAdmin = new Dashboard.dbAdmin();
DashboardAdmin.Show();
break;
}
case "Staff":
{
this.Hide();
Dashboard.dbStaff DashboardStaff = new Dashboard.dbStaff();
DashboardStaff.Show();
break;
}
default:
{
MessageBox.Show("Please enter correct username and password or register a new account!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); ;
break;
}
}
}
Connection.Close();
}
Результат, который я получил, - ничто. Я продолжаю нажимать кнопку входа и до сих пор ничего не показывал Так как пароль был зашифрован, как я могу расшифровать пароль с кодом входа выше?