/// <summary> /// Metoda obsługująca zdarzenie kliknięcia w przycisk RegisterBtn /// Metoda jest odpowiedzialna za procedurę rejestracji nowego użytkownika /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void RegisterBtn_Click(object sender, EventArgs e) { string login, pwd, pesel; login = LoginTextBox.Text; pesel = maskedTextBox1.Text; pwd = PwdTextBox.Text; if (!BindingModule.CheckPesel(pesel)) { StatusLabel.Text = "Invalid Pesel"; return; } if (LoginTextBox.Text == "" || maskedTextBox1.Text == "" || PwdTextBox.Text == "" || ConfPwdTextBox.Text == "") { //RegisterBtn.Enabled = false; StatusLabel.Text = "Provide more information"; return; } if (pwd.Equals(ConfPwdTextBox.Text)) { //wyslac ponizsze i poczekac na odpowiedz bool response = SendRegisterMsg(login, CryptoModule.HashMessage(pwd), CryptoModule.HashMessage(pesel)); if (response) { StatusLabel.Text = "Registration completed succesfully"; LoginTextBox.ReadOnly = true; maskedTextBox1.ReadOnly = true; PwdTextBox.ReadOnly = true; ConfPwdTextBox.ReadOnly = true; RegisterBtn.Enabled = false; } else { StatusLabel.Text = "Registration failed"; LoginTextBox.ResetText(); maskedTextBox1.ResetText(); PwdTextBox.ResetText(); ConfPwdTextBox.ResetText(); } } else { StatusLabel.Text = "Passwords don't match"; } }
/// <summary> /// Metoda słuzaca do rozlaczenia aktualnej rozmowy /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DisconnectBtn_Click(object sender, EventArgs e) { if (isChatOpen) { //Disconnect SendBtn.Enabled = false; MsgTextBox.Enabled = false; isChatOpen = false; EnableDisableControls(true); this.MsgTextBox.Enabled = false; this.SendBtn.Enabled = false; this.DisconnectBtn.Enabled = false; CryptoModule.RemoveUserKey(loginToConnect); cm.Send(Encoding.UTF8.GetBytes("DIC")); loginToConnect = null; WriteInLog("Disconnected. Chat terminated"); } }
/// <summary> /// Metoda wysyłająca wiadomość użytkownika /// </summary> private void TextToSendMethod() { if (!areCharsOver) { string msg = MsgTextBox.Text; byte[] encryptedMsg = CryptoModule.EncryptMsg(Encoding.UTF8.GetBytes(msg), loginToConnect); byte[] signedMsg = CryptoModule.Sign(msg); byte[] toSend = new byte[259]; byte[] prefix = Encoding.UTF8.GetBytes("MSG"); Array.Copy(prefix, 0, toSend, 0, 3); Array.Copy(encryptedMsg, 0, toSend, 3, 128); Array.Copy(signedMsg, 0, toSend, 131, 128); cm.Send(toSend); WriteInLogAsThisUser(msg); MsgTextBox.ResetText(); } }
/// <summary> /// Metoda odpowiedzialna za obsluge przychodzacych wiadomosci /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MsgService(object sender, MsgEvent e) { sb.Clear(); Decoder decoder = Encoding.UTF8.GetDecoder(); char[] chars = new char[decoder.GetCharCount(e.msg, 0, e.msg.Length)]; decoder.GetChars(e.msg, 0, e.msg.Length, chars, 0); sb.Append(chars); string message = sb.ToString(); sb.Clear(); if (String.Compare("ONLR", 0, message, 0, 4) == 0) { string onlinelist = message.Substring(4); onlinelist = onlinelist.Replace(' ', '\n'); WriteInLog("Online:\n" + onlinelist); return; } else if (String.Compare("ISOR", 0, message, 0, 4) == 0) { bool isOnline = false; string msg = null; if (String.Compare("True", 0, message, 4, 4) == 0) { isOnline = true; } if (!isOnline) { sb.Append("Connection attempt with: ").Append(loginToConnect). Append(" failed, User is offline"); msg = sb.ToString(); WriteInLog(msg); loginToConnect = null; sb.Clear(); return; } else { sb.Append("Connection attempt with: ").Append(loginToConnect). Append(" succeed"); msg = sb.ToString(); WriteInLog(msg); sb.Append("Waiting for certificate from ").Append(loginToConnect); msg = sb.ToString(); WriteInLog(msg); sb.Clear(); byte[] msgOut = BindingModule.enc.GetBytes("GCR"); cm.Send(msgOut); return; } } else if (String.Compare("GCRA", 0, message, 0, 4) == 0) { byte[] data = e.msg; byte[] certRawData = new byte[data.Length - 4]; Array.Copy(data, 4, certRawData, 0, certRawData.Length); X509Certificate2 cert = CryptoModule.CreatePublicCertFromRawData(certRawData); loginToConnect = CryptoModule.ImportKey(cert, false, false); string msg; sb.Clear(); sb.Append("Alice received certicate from ").Append(loginToConnect). Append(". Starting chat now"); msg = sb.ToString(); WriteInLog(msg); //Rozpoczecie dzialania chatu isChatOpen = true; EnableDisableControls(false); this.MsgTextBox.Enabled = true; this.SendBtn.Enabled = true; this.DisconnectBtn.Enabled = true; return; } else if (String.Compare("GCRB", 0, message, 0, 4) == 0) { byte[] data = e.msg; byte[] certRawData = new byte[data.Length - 4]; Array.Copy(data, 4, certRawData, 0, certRawData.Length); X509Certificate2 cert = CryptoModule.CreatePublicCertFromRawData(certRawData); loginToConnect = CryptoModule.ImportKey(cert, false, false); string msg; sb.Clear(); sb.Append("Bob received certicate from ").Append(loginToConnect). Append(".Connecting. Starting chat now"); msg = sb.ToString(); WriteInLog(msg); //Rozpoczecie dzialania chatu isChatOpen = true; EnableDisableControls(false); this.MsgTextBox.Enabled = true; this.SendBtn.Enabled = true; this.DisconnectBtn.Enabled = true; return; } else if (String.Compare("DIC", 0, message, 0, 3) == 0) { //Disconnect if (isChatOpen) { SendBtn.Enabled = false; MsgTextBox.Enabled = false; isChatOpen = false; EnableDisableControls(true); this.MsgTextBox.Enabled = false; this.SendBtn.Enabled = false; this.DisconnectBtn.Enabled = false; CryptoModule.RemoveUserKey(loginToConnect); loginToConnect = null; WriteInLog("Disconnected. Chat terminated"); } } else if (String.Compare("MSG", 0, message, 0, 3) == 0) { WriteInLog("MSG recived"); byte[] encrypted = new byte[128]; byte[] signed = new byte[128]; Array.Copy(e.msg, 3, encrypted, 0, 128); Array.Copy(e.msg, 131, signed, 0, 128); byte[] decrypted = CryptoModule.DecryptMsg(encrypted); if (CryptoModule.Verify(decrypted, signed, loginToConnect)) { string msg = Encoding.UTF8.GetString(decrypted); WriteInLogAsOtherUser(msg); } else { //Disconnect SendBtn.Enabled = false; MsgTextBox.Enabled = false; isChatOpen = false; EnableDisableControls(true); this.MsgTextBox.Enabled = false; this.SendBtn.Enabled = false; this.DisconnectBtn.Enabled = false; CryptoModule.RemoveUserKey(loginToConnect); cm.Send(Encoding.UTF8.GetBytes("DIC")); loginToConnect = null; WriteInLog("Disconnected. Chat terminated"); } } }
/// <summary> /// Metoda służąca do przeprowadzenia procedury logowania /// </summary> private void LoginMethod() { if (PwdTextBox.Text != "" && LoginTextBox.Text != "") { //Zebranie danych z pol okna logowania string login = LoginTextBox.Text; string pwd = PwdTextBox.Text; //Zaszyfrowanie hasla i usuniecie z pamieci byte[] pwdArray = BindingModule.enc.GetBytes(pwd); StatusLabel.Text = "Login in progress"; pwd = ""; PwdTextBox.ResetText(); //Uruchomienie modulu komunkacji if (!mw.cm.Run("localhost", "pkryserver.jumpingcrab.com")) { StatusLabel.Text = "Not able to tart comunnication module"; return; } //Wyslanie loginu i oczekiwanie na odp int n = mw.cm.SendLogin(login); //Reakcja na bledny login if (n == 0) { StatusLabel.Text = "Signing in failed, wrong login"; mw.cm.Stop(); } //Poprawny login hashowanie i wyslanie hasla else { pwdArray = CryptoModule.HashNTimes(pwdArray, --n); //send pwdArray //Poczekaj na odpowiedz // bool response = mw.cm.SendPwd(pwdArray); if (mw.cm.SendPwd(pwdArray)) { BindingModule.setLogin(login); //Udane logowanie, czekam na certyfikat StatusLabel.Text = "Waiting for certificate"; //Otrzymany certyfikat byte[] certificateRawData = mw.cm.GetCertificate(); X509Certificate2 certificate = CryptoModule.CreatePrivateCertFromRawData(certificateRawData); CryptoModule.ImportKey(certificate, true, false); CryptoModule.ImportKey(certificate, true, true); mw.EnableDisableControls(true); mw.DisableLogBtn(); mw.WriteInLog("Logged in!"); mw.cm.Run(); this.Close(); } else { StatusLabel.Text = "Signing in failed, wrong password"; } } } else { StatusLabel.Text = "Need more data to proceed"; } }