コード例 #1
0
ファイル: MainWindow.cs プロジェクト: SosnoV/PKRYLast
        /// <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");
                }
            }
        }
コード例 #2
0
ファイル: LoginWindow.cs プロジェクト: SosnoV/PKRYLast
        /// <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";
            }
        }