Click here to Skip to main content
15,889,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I recently created a login and register function in my c# windows form, I implemented a SHA1 encryption but ut failed in the decrypting part. So I was searching for a different way of password encryption, which is I found is the AES, but I'm having a hard part in implementing it since I use a database (sql server) in saving the data from the registration. I hope someone can guide me through this.

What I have tried:

I already have a registration and login functionalities, the only thing I lack in the application is the encryption and decryption for the password.

Login:
C#
private void btn_Login_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=DESKTOP-2SSIMR1;Initial Catalog=Juan Carlo SCM;Integrated Security=True");
            SqlDataAdapter sda = new SqlDataAdapter("SELECT Username, Password FROM Employee_Table WHERE Username= '" + txtUsername.Text + "' AND Password = '" + txtPassword.Text + "' AND Status='Approved'", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);

            if (dt.Rows.Count == 1)
            {
                this.Hide();
                Home hm = new Home(dt.Rows[0][0].ToString());
                hm.Show();
            }
            else
            {
                MessageBox.Show("Invalid username or Password", "Login Information", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }


Registration:
C#
private void Register_Btn_Click(object sender, EventArgs e)
        {
            if (IsValidated())
            {
                try
                {
                    if (con.State == ConnectionState.Closed)
                        con.Open();
                    if (Register_Btn.Text == "Register")
                    {
                        SqlCommand cmd = new SqlCommand("RegisterAdd", con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@mode", "Add");
                        cmd.Parameters.AddWithValue("@EmployeeID", 0);
                        cmd.Parameters.AddWithValue("@LName", txtLastName.Text.Trim());
                        cmd.Parameters.AddWithValue("@FName", txtFirstName.Text.Trim());
                        cmd.Parameters.AddWithValue("@Contact", txtContact.Text.Trim());
                        cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
                        cmd.Parameters.AddWithValue("@Username", txtUsername.Text.Trim());
                        cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
                        cmd.Parameters.AddWithValue("@Position", txtPosition.Text.Trim());
                        cmd.Parameters.AddWithValue("@Status", txtStatus.Text.Trim());

                        if (txtPassword.Text != txtConfirmPassword.Text)
                        {
                            MessageBox.Show("Password not Match!");
                            txtPassword.Text = "";
                            txtConfirmPassword.Text = "";
                        }
                        if (txtUsername.Text.Length < 3 || txtPassword.Text.Length < 4)
                        {
                            MessageBox.Show("Username or Pasword is too short");
                        }
                        else
                        {

                            cmd.ExecuteNonQuery();
                            MessageBox.Show("Recorded Successfully.");
                            con.Close();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error Message");
                    return;
                }
                finally
                {
                    con.Close();
                }
            }
        }
Posted
Updated 20-Oct-18 23:21pm

SHA is not an encryption algorithm - it is a hashing algorithm and the two are very different. Encryption can be reversed to recover the original input, hashing cannot. You can't "decrypt" an SHA1 hash value to get back the original password.

Having said that, it is exactly the right thing to use for a password!
No, seriously: encrypting passwords is insecure, because the decryption key has to be part of your app to use it - which means you are keeping the key with the data where it is easy to find. As a result, encrypted passwords are about as secure as storing them in plain text!

To use hashed passwords you get the hashed value from the DB and compare it to the hash generated from what the user input. If they match, he entered the right password. If they don't, he didn't. There is some information on how to do it here: Password Storage: How to do it.[^]

But you have a bigger, more dangerous and pressing problem: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
You need to go through your whole app and fix everywhere you do that - leave one, and your DB is at risk. And doing it on a login page? That's not just leaving your front door unlocked, that's handing the burglars the keys on your way out and telling them the alarm code...
 
Share this answer
 
Comments
Jonathan Sumilang 21-Oct-18 4:33am    
I'm planning to make it as parameterized queries as long as I got it working, I only have it made it like that to show how my login and registration work. I just need to put a function that would encrypt the password in the registration and decrypt it as the user enters the system
OriginalGriff 21-Oct-18 4:44am    
Never, ever do that. When you do, it gets forgotten. It's easier, cleaner, and simpler - not to mention quicker - to do it properly right from the start, and it makes your code more readable as well.
Jonathan Sumilang 21-Oct-18 4:40am    
or in anyway that I can make my login and register working securely, I just need to have it made
OriginalGriff 21-Oct-18 4:46am    
Wrong attitude: security is important, that's partly why the EU introduced GDPR - and you can be prosecuted for throwing it together and hoping ...
Jonathan Sumilang 21-Oct-18 4:52am    
so uhm, what should I do then?
Quote:
I implemented a SHA1 encryption but ut failed in the decrypting part.

Because SHA is not encryption, it can't be decrypted.
SHA-1 - Wikipedia[^]
Your code is using plain text password in the SQL requests, there is no attempt to encrypt it.

C#
SqlDataAdapter sda = new SqlDataAdapter("SELECT Username, Password FROM Employee_Table WHERE Username= '" + txtUsername.Text + "' AND Password = '" + txtPassword.Text + "' AND Status='Approved'", con);

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900