private void btnDelete_Click(object sender, EventArgs e) { string username = txtUsername.Text; string password = txtPassword.Text; bool userExists = false; string salt = ""; string hashedPassword = ""; string hashControl = ""; try { //user.login-file openen en lijn inlezen input = File.OpenText(path + "/user.login"); line = input.ReadLine(); char separator = ','; string[] words; //salt en hash bepalen van users in bestand while (line != null && !userExists) { lineCounter++; words = line.Split(separator); if (words[0] == username) { userExists = true; salt = words[1]; hashedPassword = words[2]; byte[] passwordBytes = Encoding.UTF8.GetBytes(password); byte[] saltBytes = Encoding.UTF8.GetBytes(salt); hashControl = PasswordStorage.HashPassword(passwordBytes, saltBytes, 50000); //Maakt nieuwe hash om te controleren } else { line = input.ReadLine(); } input.Close(); } } catch (IOException) { MessageBox.Show("Close the 'user.login'-file before logging in.", "'user.login'-file is open."); return; } if (!userExists) { lbl1.Text = "This user does not exist."; lbl2.Text = ""; return; } if (hashedPassword != hashControl) { lbl1.Text = "The password is incorrect."; lbl2.Text = ""; return; } else { DeleteUser(username); FormRegister regForm = new FormRegister(lf); regForm.Show(); this.Hide(); } }
//NIEUWE USER MAKEN private void CreateUser() { bool containsSpace = false; try { writer = File.AppendText(path + "/user.login"); //Username controleren en in user.login zetten for (int i = 0; i < username.Length; i++) { if (username[i] == ' ') { lblNotification.Text = "Username can not contain a space."; containsSpace = true; return; } } //Password controleren for (int i = 0; i < password.Length; i++) { if (password[i] == ' ') { lblNotification.Text = "Password can not contain a space."; containsSpace = true; return; } } if (!containsSpace) { writer.Write(username + ","); } //Salt maken en in user.login zetten string salt = PasswordStorage.GenerateSalt(); writer.Write(salt + ","); byte[] saltBytes = Encoding.UTF8.GetBytes(salt); byte[] passwordBytes = Encoding.UTF8.GetBytes(password); //Password + salt hashen en in user.login zetten string hashedPassword = PasswordStorage.HashPassword(passwordBytes, saltBytes, 50000); writer.WriteLine(hashedPassword); loginForm.userCreated = true; //Public en private key van deze gebruiker genereren RSAWithRSAParameterKey rsaParams = new RSAWithRSAParameterKey(username); rsaParams.GeneratePrivatePublicKeys(); CreateFolder(); //Folders voor de gebruiker maken } catch (IOException) { MessageBox.Show("Close the user.login-file before registering.", "Close user.login-file."); return; } finally { writer.Close(); if (!containsSpace) { this.Close(); } } }
//INLOGGEN DOOR KLIK OP LOGIN-BUTTON private void btnLogin_Click_1(object sender, EventArgs e) { userExists = false; //Terug op false zetten voor het geval het nog op true staat username = txtUsername.Text; password = txtPassword.Text; userExists = false; string hashControl = ""; //Zoeken naar gebruikersnaam in login-file (om vervolgens in te loggen) try { //user.login-file openen en eerste lijn lezen reader = File.OpenText(path + "/user.login"); string line = reader.ReadLine(); char separator = ','; string[] words; while (line != null && !userExists) { words = line.Split(separator); //Username-check if (words[0] == username) { userExists = true; //Salt en byte opslaan en omzetten naar byte-array salt = words[1]; byte[] saltBytes = Encoding.UTF8.GetBytes(salt); hashedPassword = words[2]; byte[] passwordBytes = Encoding.UTF8.GetBytes(password); hashControl = PasswordStorage.HashPassword(passwordBytes, saltBytes, 50000); //Maakt nieuwe hash om te controleren //of deze overeenkomt met die wat er werkelijk staat } else { line = reader.ReadLine(); } } } catch (IOException) { MessageBox.Show("Close the user.login-file before logging in.", "Close user.login-file."); return; } finally { reader.Close(); } //labelopmaak aanpassen lblNotification.ForeColor = Color.Red; lblNotification.Font = new Font(lblNotification.Font, FontStyle.Bold); if (!userExists) { lblNotification.Text = "This user does not exist."; return; } if (hashedPassword != hashControl) { lblNotification.Text = "The password is incorrect."; return; } else { MainWindow mainWindow = new MainWindow(this); mainWindow.Show(); this.Hide(); } }