Exemple #1
0
        public CommonChat()
        {
            InitializeComponent();

            FriendsChat = new Dictionary <string, ListBox>();
            Client      = new UdpClient();
            _newMessage = new SoundPlayer(@"..\..\Resources\newMessage.wav");

            TabControlStatic = tabControl;
            MsgBoxStatic     = msgBox;
            SendBtnStatic    = sendBtn;

            tabControl.TabPages.Clear();

            RSATools.GenerateKeys(out LocalPublicKey, out _localPrivateKey);
            Database.GetFriends();

            LocalPort = Database.GetLocalPort();

            // Si aucun ami n'a été ajouté
            if (TabControlStatic.TabCount == 0)
            {
                sendBtn.Enabled = false;
                msgBox.Enabled  = false;
            }

            _threadLoop = true;
            Restart_Thread();
        }
Exemple #2
0
        /// <summary>
        /// Ecoute en permanance le trafic sur le port d'écoute
        /// </summary>
        private void Listen()
        {
            UdpClient server = null;

            try
            {
                server = new UdpClient(LocalPort);
            }
            catch
            {
                // impossible d'accéder au port
                Thread.Sleep(100);
                return;
            }

            server.Client.ReceiveTimeout = 1000;

            while (_threadLoop)
            {
                try
                {
                    IPEndPoint ep           = new IPEndPoint(IPAddress.Any, LocalPort);
                    byte[]     data         = server.Receive(ref ep);
                    string     uncryptedMsg = Encoding.UTF8.GetString(data);

                    // Si le message en dur (avant décryptage) et qu'il provient du bon EndPoint.
                    if (uncryptedMsg == "RESET_KEY")
                    {
                        Invoke(new Action <string, string>(Database.SetKey), Database.GetNameByIP(ep.Address), "WAITING_FOR_KEY");
                    }

                    const string beginning = "<RSAKeyValue><Modulus>";
                    const string middle    = "</Modulus><Exponent>";
                    const string ending    = "</Exponent></RSAKeyValue>";

                    // Vérifie que c'est une clé publique valide
                    if (data.Length == 415)
                    {
                        if (uncryptedMsg.Substring(0, beginning.Length) == beginning && uncryptedMsg.Substring(uncryptedMsg.Length - ending.Length - 4 - middle.Length, middle.Length) == middle && uncryptedMsg.Substring(uncryptedMsg.Length - ending.Length, ending.Length) == ending)
                        {
                            // Remplace la clé de l'ami actuel
                            Invoke(new Action <string, string>(Database.SetKey), Database.GetNameByIP(ep.Address), uncryptedMsg);
                            msgBox.Enabled = true;
                            continue;
                        }
                    }

                    string decryptedMsg = Encoding.UTF8.GetString(RSATools.RSADecrypt(data, _localPrivateKey, false));

                    Invoke(new Action <string, string>(AddLog), decryptedMsg, Database.GetNameByIP(ep.Address));
                }
                catch
                {
                    // do nothing
                }
            }

            server.Close();
        }
Exemple #3
0
        /// <summary>
        /// Envoie un message à l'ami actif
        /// </summary>
        /// <param name="msg">Message à envoyer</param>
        /// <param name="isKeyMsg">Le message est-il l'envoi de notre clé public ou la demande de reset de clé (pour ne pas l'écrire)</param>
        /// <param name="toEveryone">Le message s'envoie-il à tous les contacts</param>
        public static void SendMessage(string msg, bool isKeyMsg, bool toEveryone)
        {
            if (CommonChat.TabControlStatic.TabPages.Count != 0)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("users.xml");

                // Valeurs par défaut
                int       remotePort      = 5053;
                string    remotePublicKey = CommonChat.LocalPublicKey;
                IPAddress remoteIPAddress = IPAddress.Broadcast;

                foreach (XmlNode node in doc.DocumentElement)
                {
                    if (node.Name == "friend")
                    {
                        string name = node.Attributes[0].InnerText;
                        if (name == CommonChat.TabControlStatic.SelectedTab.Text)
                        {
                            // load his public key to encrypt message
                            remotePublicKey = node.ChildNodes[2].InnerText;
                            remotePort      = Int32.Parse(node.ChildNodes[1].InnerText);
                            remoteIPAddress = IPAddress.Parse(node.ChildNodes[0].InnerText);
                            break;
                        }
                    }
                }

                byte[] data = Encoding.UTF8.GetBytes(msg);
                byte[] encryptedData;

                if (isKeyMsg)
                {
                    // n'encrypte pas le message si c'est une clé
                    encryptedData = data;
                }
                else
                {
                    encryptedData = RSATools.RSAEncrypt(data, remotePublicKey, false);
                }

                if (toEveryone)
                {
                    foreach (string friendName in GetFriendNames())
                    {
                        // Se connecte à l'ami actif
                        ConnectToFriend(friendName);

                        if (!isKeyMsg)
                        {
                            CommonChat.FriendsChat[CommonChat.TabControlStatic.SelectedTab.Text].Items.Add("Moi [" + DateTime.Now + "] > " + msg);
                        }

                        CommonChat.Client.Send(encryptedData, encryptedData.Length);
                        CommonChat.MsgBoxStatic.Text = "";
                    }
                }
                else
                {
                    // Se connecte à l'ami actif
                    ConnectToFriend(CommonChat.TabControlStatic.SelectedTab.Text);

                    if (!isKeyMsg)
                    {
                        CommonChat.FriendsChat[CommonChat.TabControlStatic.SelectedTab.Text].Items.Add("Moi [" + DateTime.Now + "] > " + msg);
                    }

                    CommonChat.Client.Send(encryptedData, encryptedData.Length);
                    CommonChat.MsgBoxStatic.Text = "";
                }

                CommonChat.Client.Close();
                CommonChat.MsgBoxStatic.Select();
            }
        }