so I am having a problem with this script I have. I created a new class in my project and it is named AesCryp.cs
and it comes with both encryption and decryption methods. Here is the script:
class AesCryp
{
public static string IV = "xxxxxxxxxxxxxxxx";
public static string Key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
public static string Encrypt(string decrypted)
{
byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(decrypted);
AesCryptoServiceProvider encdec = new AesCryptoServiceProvider();
encdec.BlockSize = 128;
encdec.KeySize = 256;
encdec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
encdec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
encdec.Padding = PaddingMode.PKCS7;
encdec.Mode = CipherMode.CBC;
ICryptoTransform icrypt = encdec.CreateEncryptor(encdec.Key, encdec.IV);
byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
icrypt.Dispose();
return Convert.ToBase64String(enc);
}
public static string Decrypt(string encrypted)
{
byte[] encbytes = Convert.FromBase64String(encrypted);
AesCryptoServiceProvider encdec = new AesCryptoServiceProvider();
encdec.BlockSize = 128;
encdec.KeySize = 256;
encdec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
encdec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
encdec.Padding = PaddingMode.PKCS7;
encdec.Mode = CipherMode.CBC;
ICryptoTransform icrypt = encdec.CreateDecryptor(encdec.Key, encdec.IV);
byte[] dec = icrypt.TransformFinalBlock(encbytes, 0, encbytes.Length);
icrypt.Dispose();
return ASCIIEncoding.ASCII.GetString(dec);
}
}
I have made it when I create a new account, it will encrypt the password and insert it into the database. Here is the register script:
public bool Register(string user, string pass)
{
string query = $"INSERT INTO users (ID, Username, Password) VALUES ('', '{user}', '{pass}');";
try
{
if (OpenConnection())
{
MySqlCommand cmd = new MySqlCommand(query, db);
try
{
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
return false;
}
}
else
{
db.Close();
return false;
}
}
catch (Exception ex)
{
db.Close();
return false;
}
}
and here is the SignUp button script:
private void SignUp_Click(object sender, EventArgs e)
{
string user = usernameTxt.Text;
string pass = AesCryp.Encrypt(passwordTxt.Text);
if(Register(user, pass))
{
MessageBox.Show($"User {user} has been created!");
}
else
{
MessageBox.Show($"User {user} has not been created!");
}
}
However, I am just really confused on how I am going to make it so it decrypts the encrypted password in the database when I try to login. This is my login script without any decryption (because I'm not entirely sure how I would do it)
public bool IsLogin(string user, string pass)
{
string query = $"SELECT * FROM users WHERE Username='{user}' AND Password='{pass}';";
try
{
if (OpenConnection())
{
MySqlCommand cmd = new MySqlCommand(query, conn);
MySqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
reader.Close();
conn.Close();
return true;
}
else
{
reader.Close();
conn.Close();
return false;
}
}
else
{
conn.Close();
return false;
}
}
catch (Exception ex)
{
conn.Close();
return false;
}
}
and here is the Login button script:
private void Login_Click(object sender, EventArgs e)
{
string user = usernameTxt.Text;
string pass = passwordTxt.Text;
if (IsLogin(user, pass))
{
MessageBox.Show($"Welcome {user}!");
}
else
{
MessageBox.Show("Username or password is incorrect!");
}
}
If anyone could help me out, then that would be great. Thank you!