private protected void CreateAdminIfNotExit() { DB db = new DB(); SQLiteCommand command = new SQLiteCommand($"SELECT admin FROM users", db.getConnection()); db.openConnection(); try { command.ExecuteNonQuery(); } catch { SQLiteCommand command1 = new SQLiteCommand("INSERT INTO users(login, password, cryptokey) VALUES(@login, @password, @cryptokey)", db.getConnection()); //для генерации ключа Crypt crypt = new Crypt(); string key = crypt.GenerateKey(); command1.Parameters.AddWithValue("@login", "admin").Size = 50; command1.Parameters.AddWithValue("@password", "admin").Size = 50; command1.Parameters.AddWithValue("@cryptokey", key).Size = crypt.keyLength; db.openConnection(); try { command1.ExecuteNonQuery(); } catch { //MessageBox.Show("Произошла ошибка"); } } db.closeConnection(); }
private void bRegister_Click(object sender, EventArgs e) { DB db = new DB(); string login = tbLogin.Text; string password = tbPassword.Text; if (login == "") { MessageBox.Show("Введите логин"); return; } if (password == "") { MessageBox.Show("Введите пароль"); return; } if (isUserExists()) { MessageBox.Show("Пользователь с таким логином уже существует, введите другой."); return; } SQLiteCommand command = new SQLiteCommand("INSERT INTO users(login, password, cryptokey) VALUES(@login, @password, @cryptokey)", db.getConnection()); //для шифрования пароля Crypt crypt = new Crypt(); string key = crypt.GenerateKey(); string hash = crypt.GetHash(password); command.Parameters.AddWithValue("@login", login).Size = 50; command.Parameters.AddWithValue("@password", crypt.CryptStr(hash, key)).Size = 50; command.Parameters.AddWithValue("@cryptokey", key).Size = crypt.keyLength; SQLiteCommand command2 = new SQLiteCommand($"CREATE TABLE {login} (docname nvarchar(50) NOT NULL, doc ntext NOT NULL);", db.getConnection()); db.openConnection(); if (command.ExecuteNonQuery() == 1) { command2.ExecuteNonQuery(); MessageBox.Show("Аккаунт успешно создан"); } else { MessageBox.Show("Произошла ошибка"); } db.closeConnection(); this.Hide(); if (login == "admin") { Admin admin = new Admin(); admin.Show(); return; } TextEncrypter textEncrypter = new TextEncrypter(); textEncrypter.username = login; if (login != "admin") { textEncrypter.admin = false; } textEncrypter.Show(); }
private void bChangePass_Click(object sender, EventArgs e) { string password0 = tbOldPass.Text; string password1 = tbNewPass.Text; string password2 = tbConfirmPass.Text; if (password0 == "" || password0 == "Текущий пароль") { MessageBox.Show("Введите текущий пароль"); ResetFields(); return; } if (password1 == "" || password1 == "Новый пароль") { MessageBox.Show("Введите новый пароль"); ResetFields(); return; } if (password2 == "" || password2 == "Подтвердите пароль") { MessageBox.Show("Подтвердите пароль"); ResetFields(); return; } if (password1 != password2) { MessageBox.Show("Пароли не совпадают"); ResetFields(); return; } if (CheckPass(username, password0) == false) { MessageBox.Show("Ошибка при вводе текущего пароля"); ResetFields(); return; } DB db = new DB(); //для шифрования пароля Crypt crypt = new Crypt(); string key = crypt.GetKey(username); string hash = crypt.GetHash(password2); SQLiteCommand command = new SQLiteCommand($"UPDATE users SET password=@uP WHERE login = @uL", db.getConnection()); command.Parameters.AddWithValue("@uP", crypt.CryptStr(hash, key)).Size = 50; command.Parameters.AddWithValue("@uL", username).Size = 50; db.openConnection(); if (command.ExecuteNonQuery() == 1) { MessageBox.Show($"Новый пароль для пользователя {username} был успешно создан"); db.closeConnection(); this.Close(); return; } else { MessageBox.Show("Произошла ошибка при записи пароля"); ResetFields(); } db.closeConnection(); ResetFields(); this.Hide(); }