/// <summary>
        /// Przy zdarzeniu odebrania wiadomości pobiera ją, wypisuje do chatu bądź do obszaru logów
        /// </summary>
        /// <param name="result">zdarzenie</param>
        private void MessageCallback(IAsyncResult result)
        {
            if (receive)
            {
                try
                {
                    this.Dispatcher.Invoke(() =>
                    {
                        client.MessageCallback(result);

                        string msg       = client.ReturnMessage();
                        char firstLetter = msg.ElementAt(0);
                        string msgCut    = msg.Substring(1, msg.Length - 1);


                        switch (firstLetter)
                        {
                        case 'M':
                            client.sign.SetClientModulus(msgCut);
                            AddLogs(msg.Substring(1, msg.Length - 1), 3);
                            break;

                        case 'E':
                            client.sign.SetClientExponent(msgCut);
                            AddLogs(msgCut, 4);
                            gotKeys = true;
                            break;

                        case 'D':
                            string aesRes = client.aes.removeSalt(msgCut);
                            AddLogs(aesRes, 20);
                            aesRes = client.aes.DoAESDec(aesRes);
                            lock (List)
                            {
                                List.AppendText("Friend: " + aesRes + "\n");
                                List.SelectionStart = List.Text.Length;
                                List.ScrollToEnd();
                            }
                            break;

                        case 'K':
                            client.SetEncryptMsg(msgCut);
                            AddLogs(msgCut, 5);
                            break;

                        case 'S':
                            client.SetEncryptSig(msgCut);
                            AddLogs(msgCut, 6);
                            //Convert.ToBase64String(client.encryptMsg) + "\n\n" +
                            AddLogs(client.VerifyMsg(), 10);

                            authenticationCompleted = client.protocol.CheckNumbers(client.VerifyMsg());
                            AddLogs(authenticationCompleted.ToString(), 12);

                            gotSignature = true;

                            if (authenticationCompleted == true)
                            {
                                SendMsg.IsEnabled = true;
                                Message.IsEnabled = true;
                                client.aes.setSalt(client.protocol.GetSecretKey());
                            }
                            else
                            {
                                MessageBox.Show("Authentication went wrong.");
                            }

                            break;

                        case 'P':
                            client.protocol.SetPrimeNumber(Int32.Parse(msgCut));
                            AddLogs(msgCut, 7);
                            break;

                        case 'R':
                            client.protocol.SetPrimitiveRoot(Int32.Parse(msgCut));
                            AddLogs(msgCut, 8);
                            client.protocol.CreateNumberToSend();
                            AddLogs(client.protocol.GetNumberToSend(), 11);
                            break;

                        case 'B':
                            client.protocol.SetReceivedNumber(msgCut);
                            client.protocol.CalculateReceivedNumber();

                            gotNumber = true;
                            AddLogs(String.Concat(msgCut, " ", client.protocol.GetSecretKey()), 9);
                            break;

                        case 'Q':
                            string afterDecryption = client.sign.DecryptData(Convert.FromBase64String(msgCut));
                            bool isVerified        = client.sign.CheckNumbers(afterDecryption, 1);
                            AddLogs(isVerified.ToString(), 14);

                            break;

                        case 'T':
                            halfMsg = client.sign.DecryptData(Convert.FromBase64String(msgCut));
                            //isVerified = client.sign.CheckNumbers(afterDecryption, 0);
                            //AddLogs(isVerified.ToString(), 3);
                            break;

                        case 'U':
                            afterDecryption = String.Concat(halfMsg, client.sign.DecryptData(Convert.FromBase64String(msgCut)));
                            isVerified      = client.sign.CheckNumbers(afterDecryption, 0);
                            AddLogs(isVerified.ToString(), 15);

                            break;

                        case 'V':
                            client.aes.setAesIV(msgCut);
                            break;

                        case 'W':
                            client.aes.setAesKeys(msgCut);
                            break;

                        default:
                            break;
                        }

                        buffer = new byte[1024];
                        mySocket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref endRemote,
                                                  new AsyncCallback(MessageCallback), buffer);
                    });
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
示例#2
0
        /// <summary>
        /// Przy zdarzeniu odebrania wiadomości pobiera ją, wypisuje do chatu bądź do obszaru logów
        /// </summary>
        /// <param name="result">zdarzenie</param>
        private void MessageCallback(IAsyncResult result)
        {
            if (receive)
            {
                try
                {
                    this.Dispatcher.Invoke(() =>
                    {
                        client.MessageCallback(result);

                        string msg       = client.ReturnMessage();
                        char firstLetter = msg.ElementAt(0);
                        string msgCut    = msg.Substring(1, msg.Length - 1);


                        switch (firstLetter)
                        {
                        case 'M':
                            client.sign.SetClientModulus(msgCut);
                            AddLogs(msg.Substring(1, msg.Length - 1), 3);
                            break;

                        case 'E':
                            client.sign.SetClientExponent(msgCut);
                            AddLogs(msgCut, 4);
                            break;

                        case 'D':
                            lock (List)
                            {
                                List.AppendText("Friend: " + msg + "\n");
                                List.SelectionStart = List.Text.Length;
                                List.ScrollToEnd();
                            }
                            break;

                        case 'K':
                            client.SetEncryptMsg(msgCut);
                            AddLogs(Convert.ToBase64String(client.encryptMsg) + "\n\n" + msgCut, 5);
                            break;

                        case 'S':
                            gotSignature = true;
                            client.SetEncryptSig(msgCut);
                            AddLogs(msgCut, 6);
                            AddLogs(client.VerifyMsg(), 10);
                            break;

                        case 'P':
                            client.protocol.SetPrimeNumber(Int32.Parse(msgCut));
                            AddLogs(msgCut, 7);
                            break;

                        case 'R':
                            client.protocol.SetPrimitiveRoot(Int32.Parse(msgCut));
                            AddLogs(msgCut, 8);
                            break;

                        case 'B':
                            client.protocol.SetReceivedNumber(msgCut);
                            client.protocol.CalculateReceivedNumber();
                            AddLogs(String.Concat(msgCut, " ", client.protocol.GetSecretKey()), 9);
                            break;

                        case 'Q':
                            client.sign.SetClientQ(msgCut);
                            AddLogs(msgCut, 11);
                            break;

                        case 'T':
                            client.sign.SetClientP(msgCut);
                            AddLogs(msgCut, 12);
                            break;

                        case 'U':
                            client.sign.SetClientDP(msgCut);
                            AddLogs(msgCut, 13);
                            break;

                        case 'V':
                            client.sign.SetClientDQ(msgCut);
                            AddLogs(msgCut, 14);
                            break;

                        case 'W':
                            client.sign.SetClientInverseQ(msgCut);
                            AddLogs(msgCut, 15);
                            break;

                        case 'X':
                            client.sign.SetClientD(msgCut);
                            AddLogs(msgCut, 16);
                            gotKeys = true;
                            break;

                        default:
                            break;
                        }

                        buffer = new byte[1024];
                        mySocket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref endRemote,
                                                  new AsyncCallback(MessageCallback), buffer);
                    });
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }