1
Answer

Beginner needs help with passwords

Ask a question
Stefan

Stefan

15y
2.2k
1
Can anyone show me how to encrypt and store a password in a database. I am currently storing them in clear text in a text file.
here's my code:

this is the form I use to ask for input:

private void button1_Click(object sender, EventArgs e)
        {
            string username = textBox1.Text;
            string password = textBox2.Text;
           
            using (StreamWriter wr = System.IO.File.AppendText("C:\\pass.txt"))
            {
                wr.WriteLine(username + "," + password);
            }
            this.Close();
        }

this is the log in form:

private void button1_Click(object sender, EventArgs e)
        {
            using (StreamReader reader = new StreamReader("C:\\pass.txt"))
            {
                string[] allLines = File.ReadAllLines(@"C:\pass.txt");
                string[] usernames = new string[allLines.Length];
                string[] passwords = new string[allLines.Length];

                for (int i = 0; i < allLines.Length; i++)
                {
                    string[] lineSplit = allLines[i].Split(',');
                    usernames[i] = lineSplit[0];
                    passwords[i] = lineSplit[1];
                }
                string username = textBox1.Text;
                string password = textBox2.Text;

                for (int i = 0; i < usernames.Length; i++)
                    if (usernames[i] == username)
                        if (passwords[i] == password)
                        {
                            this.Hide();
                            Form3 frm = new Form3();
                            frm.Show();
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Incorrect username/password.");
                            return;
                        }

                MessageBox.Show("Incorrect username/password.");
            }
        }

Any help will be appreciated.

Answers (1)