示例#1
0
 /// <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");
     }
 }
示例#2
0
        /// <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");
                }
            }
        }