Beispiel #1
0
        public void updateGrid()
        {
            int   brojac   = -1;          //counter used to count the id of the datagridview field
            login fasdorm1 = new login();

            masterusername = fasdorm1.getuser();
            masterpassword = fasdorm1.getpassword();
            //get the username and password of the user
            dataGridView1.Rows.Clear();
            //clear all the rows
            SQLiteConnection dbConnection;

            dbConnection =
                new SQLiteConnection("Data Source=" + fajl() + ";Version=3;");
            try
            {
                //connect to the file
                dbConnection.Open();
                int              idbroj       = 1;                                //promenliva za skladiranje na id brojot od posledniot rekord
                string           maxid        = "SELECT MAX(ID) FROM passwords;"; //komanda za selektiranje na posledniot rekord
                string           sql          = "SELECT * FROM passwords ORDER BY id ";
                SQLiteCommand    maxidkomanda = new SQLiteCommand(maxid, dbConnection);
                SQLiteDataReader reader1      = maxidkomanda.ExecuteReader();
                while (reader1.Read())
                {
                    idbroj = reader1.GetInt32(0);
                    break;
                    //get the highest ID number from the records, as in the last record entered
                }

                reader1.Close();
                SQLiteCommand    command = new SQLiteCommand(sql, dbConnection);
                SQLiteDataReader reader  = command.ExecuteReader();
                while (reader.Read())
                {
                    //    if (int.Parse(reader["id"].ToString()) <= idbroj )
                    //    {
                    if (reader["id"].ToString() != "1")             //if the record's ID isn't 1
                    //because the first record is used to store the encrypted password
                    {
                        if (reader["visible"].ToString() == "1")     //if the record's visible field is set to 1
                        //because the "deleted" record's IDs are set to 0
                        {
                            brojac++;
                            //increment the counter (move to the next row of dataGridView)
                            dbid       = reader["id"].ToString();
                            dburl      = reader["URL"].ToString();
                            dbname     = reader["name"].ToString();
                            dbusername = reader["username"].ToString();
                            dbpassword = reader["password"].ToString();
                            dbnotes    = reader["notes"].ToString();
                            //get the record's data and turn it into strings

                            dataGridView1.Rows.Add();
                            dataGridView1.Rows[brojac].Cells[0].Value = (int.Parse(dbid)).ToString();
                            dataGridView1.Rows[brojac].Cells[1].Value = Cryptography.Decrypt(dburl, masterpassword);
                            dataGridView1.Rows[brojac].Cells[2].Value = Cryptography.Decrypt(dbname, masterpassword);
                            dataGridView1.Rows[brojac].Cells[3].Value = Cryptography.Decrypt(dbusername, masterpassword);
                            dataGridView1.Rows[brojac].Cells[4].Value = Cryptography.Decrypt(dbpassword, masterpassword);
                            dataGridView1.Rows[brojac].Cells[5].Value = Cryptography.Decrypt(dbnotes, masterpassword);
                            //input the record's data into the dataGridView table after it gets decrypted with the password gotten from the login form
                        }
                    }
                }

                dbConnection.Dispose();
                dbConnection.Close();
                reader.Close();
                //close the connection
            }
            catch (Exception ex)
            {
                // MessageBox.Show(ex.Message);
            }
        }
Beispiel #2
0
        private void proceed()
        {
            usernameTB.Text.Trim(); passwordTB.Text.Trim(); confirmPWTB.Text.Trim(); // Remove whitespace from the input entered into the textboxes.
            string databasefajl = Appdatafolder() + "\\" + usernameTB.Text + ".sqlite";

            //TODO: Rework authentication logic
            #region checked
            if (newUserCB.Checked)                                                      //If they selected the register option
            {
                if (passwordTB.Text.Equals(confirmPWTB.Text, StringComparison.Ordinal)) //Check if both of the passwords are the same
                {
                    if (!string.IsNullOrWhiteSpace(usernameTB.Text))                    //Check if the first password field is empty
                    {
                        if (!string.IsNullOrWhiteSpace(passwordTB.Text))                //Check if the second password field is empty
                        {
                            if (!File.Exists(databasefajl))                             //If the file doesn't exist already, as in the username hasn't been registered
                            {
                                sharedSecret = passwordTB.Text;
                                string enkriptirandavid = Cryptography.Encrypt(sharedSecret, passwordTB.Text); //Encrypt the password with itself
                                SQLiteConnection.CreateFile(databasefajl);                                     //Create a db file in %appdata% named username.sqlite
                                SQLiteConnection dbConnection;
                                dbConnection =
                                    new SQLiteConnection("Data Source=" + databasefajl + ";Version=3;");
                                using (var myconnection = new SQLiteConnection(dbConnection))
                                {
                                    myconnection.Open(); //Open the .sqlite file

                                    try
                                    {
                                        string sqlinsert = "insert into passwords (url, name) values ('" + enkriptirandavid + "','" + enkriptirandavid + "');";
                                        //sqlinsert.Parameters.AddWithValue("@url", enkriptirandavid);
                                        SQLiteCommand sqlinsert1 = new SQLiteCommand(sqlinsert, myconnection);
                                        string        komanda    = "create table passwords (id integer primary key autoincrement,URL varchar(150), name varchar(150)" +
                                                                   ",username varchar(150), password varchar(150), notes varchar(1500), visible integer)";
                                        SQLiteCommand izvrsikomanda2 = new SQLiteCommand(komanda, myconnection);
                                        izvrsikomanda2.ExecuteNonQuery(); //Create a table named passwords
                                        sqlinsert1.ExecuteNonQuery();     //Fill the first record's first 2 fields with the encrypted password
                                        myconnection.Close();
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message);
                                    }

                                    newUserCB.Checked = false;
                                    MessageBox.Show("Registration was successful"); //Successful registration
                                    proceed();
                                }
                            }
                            else
                            {
                                MessageBox.Show("That your name is already taken."); //Username already exists
                            }
                        }
                        else
                        {
                            MessageBox.Show("A password must be entered."); //No password entered
                        }
                    }
                    else
                    {
                        MessageBox.Show("A username must be entered."); //No username entered
                    }
                }
                else
                {
                    MessageBox.Show("The passwords do not match each other."); //Password mismatch
                }
            }
            #endregion
            else
            {
                if (File.Exists(databasefajl)) //If the file exists already, when the username has been registered
                {
                    sharedSecret = passwordTB.Text;
                    SQLiteConnection dbConnection;
                    dbConnection =
                        new SQLiteConnection("Data Source=" + databasefajl + ";Version=3;");
                    dbConnection.Open(); //Connect to the .sqlite file
                    string           sql     = "SELECT * FROM passwords ORDER BY id ";
                    SQLiteCommand    command = new SQLiteCommand(sql, dbConnection);
                    SQLiteDataReader reader  = command.ExecuteReader(); //Get the records inside
                    while (reader.Read())
                    {                                                   //If (textBox2.Text == Cryptography.Decrypt(reader["URL"].ToString(), sharedsecret))
                        if (Cryptography.Encrypt(passwordTB.Text, sharedSecret) == reader["URL"].ToString())
                        //Encrypt the entered password with the one that is in the first record of the file
                        {
                            username = usernameTB.Text;
                            password = passwordTB.Text;
                            reader.Close();
                            dbConnection.Close();
                            OverviewForm form2 = new OverviewForm(); //Close the SQLITE connection and open the main form.
                            Hide();
                            form2.ShowDialog(this);
                            Close();
                            break;
                        }
                        else
                        {
                            MessageBox.Show("Погрешна лозинка"); //Wrong password
                            reader.Close();
                            dbConnection.Close();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Корисничкото име не е регистрирано"); //Username not registered
                }
            }
        }
Beispiel #3
0
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                if (dataGridView1.SelectedRows.Count < 2)
                {
                    //if only one row has been selected from the dataGridView table

                    int selectedid = int.Parse(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["id"].Value.ToString());
                    //get the selected row's ID from the hidden ID column
                    SQLiteConnection dbConnection;
                    dbConnection =
                        new SQLiteConnection("Data Source=" + fajl() + ";Version=3;");
                    try
                    {
                        //connect to the file
                        dbConnection.Open();
                        string sql = "SELECT * FROM passwords WHERE ID=" + selectedid + ";";
                        //select the record with the ID gotten from before
                        SQLiteCommand    command = new SQLiteCommand(sql, dbConnection);
                        SQLiteDataReader reader  = command.ExecuteReader();
                        while (reader.Read())
                        {
                            textBox1.Text = Cryptography.Decrypt(reader["URL"].ToString(), masterpassword);
                            textBox2.Text = Cryptography.Decrypt(reader["name"].ToString(), masterpassword);
                            textBox3.Text = Cryptography.Decrypt(reader["username"].ToString(), masterpassword);
                            textBox4.Text = Cryptography.Decrypt(reader["password"].ToString(), masterpassword);
                            textBox5.Text = Cryptography.Decrypt(reader["notes"].ToString(), masterpassword);
                        }
                        //fill the textboxes with the decrypted values of the record
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    //make the textfields visible
                    #region visible
                    label1.Visible   = true;
                    label2.Visible   = true;
                    label3.Visible   = true;
                    label4.Visible   = true;
                    label5.Visible   = true;
                    textBox1.Visible = true;
                    textBox2.Visible = true;
                    textBox3.Visible = true;
                    textBox4.Visible = true;
                    textBox5.Visible = true;
                    button4.Visible  = true;
                    button5.Visible  = true;
                    #endregion
                }
            }
            else
            {
                //make the text fields invisible
                #region invisible
                label1.Visible   = false;
                label2.Visible   = false;
                label3.Visible   = false;
                label4.Visible   = false;
                label5.Visible   = false;
                textBox1.Visible = false;
                textBox2.Visible = false;
                textBox3.Visible = false;
                textBox4.Visible = false;
                textBox5.Visible = false;
                button4.Visible  = false;
                button5.Visible  = false;
                #endregion
            }
        }