Ejemplo n.º 1
0
        //单击发送按钮,开始通信
        private void btnSend_Click(object sender, EventArgs e)
        {
            //开始检查是否选中了通信对象
            if (-1 == this.lbxComClient.SelectedIndex)
            {
                MessageBox.Show("    请选择需要通信的用户!    ");
                return;
            }


            //检测是否在文本框中输入了内容
            if (string.Empty == this.txtInput.Text)
            {
                MessageBox.Show("      不能发送空消息!      ");
                return;
            }


            //开始检查是否选中了通信对象
            if (this.txtInput.Text.Length > 100)
            {
                MessageBox.Show("    输入的文本不要大于100个字符!    ");
                return;
            }


            //发送通信报文



            string reqMsg = null;

            //目的地址用户名
            //源地址用户名
            string username1 = hostIP.Replace('.', 'a') + "b" + hostPort;
            string username2 = this.lbxComClient.SelectedItem.ToString();
            //找出comClient
            int       i = 0;
            ComClient c = null;

            for (i = 0; i < comClients.Count; i++)
            {
                if (username2 == ((ComClient)comClients[i]).Username)
                {
                    c = (ComClient)comClients[i];
                    break;
                }
            }

            if (i >= comClients.Count)
            {
                MessageBox.Show("      通信对象已经不存在!      ");
                return;
            }
            else
            {
                //通信内容
                string msg = new AES(c.Kaes).AesEncipher(this.txtInput.Text);
                //构造报文

                reqMsg = "7|" + username1 + "|" + username2 + "|" + msg;

                //发送报文
                sendMessage(aes.AesEncipher(reqMsg), IPAddress.Parse(serverIP), int.Parse(serverPort));

                //页面显示
                this.txtShowMessage.AppendText("\r\n" + username1 + "—>" + this.lbxComClient.SelectedItem.ToString() + "  " + TimeUtil.getNowTime() + "\r\n    " + this.txtInput.Text);
                this.txtInput.Clear();
                this.txtInput.Focus();
            }
        }
Ejemplo n.º 2
0
        private void ReceiveData()
        {
            UdpClient udpClient = new UdpClient(int.Parse(hostPort));
            //远程主机节点
            IPEndPoint remote = null;

            // RSA rsa = new RSA();
            // AES aes = new AES(KDCaes);

            //接收从远程主机发送过来的信息
            while (true)
            {
                //关闭udpClient时此句会产生异常
                byte[] bytes  = udpClient.Receive(ref remote);
                string recStr = aes.AesDecipher(Encoding.UTF8.GetString(bytes, 0, bytes.Length)).Trim();



                string[] recStrArr = recStr.Split('|');
                if ("1" == recStrArr[0])
                {
                    //this.txtConInfo.AppendText("\r\n" + string.Format("来自{0}:{1}", remote, recStr));
                    if ("CONNECT" == recStrArr[1])
                    {
                        this.txtConInfo.AppendText("\r\n" + hostIP + ":" + hostPort + " 服务器连接成功");
                        this.btnRegistration.Enabled = true;
                        //this.btnConnect.Enabled = false;
                    }
                    else
                    {
                        //服务器连接失败
                    }
                }

                if ("2" == recStrArr[0])
                {
                    this.txtRegInfo.AppendText("\r\n" + string.Format("来自{0}:{1}", remote, recStr));
                    string username = hostIP.Replace('.', 'a') + "b" + hostPort;

                    string str = rsa.Decryption(recStrArr[1], KDCe, KDCn);
                    if ("exist" == str)
                    {
                        this.txtRegInfo.AppendText("\r\n" + username + "用户已经注册成功。。。");
                    }
                    else if (username == str)
                    {
                        this.txtRegInfo.AppendText("\r\n" + "注册成功。。。");
                        this.btnGetCertificate.Enabled = true;
                    }
                    else
                    {
                        //注册失败
                    }
                }

                if ("3" == recStrArr[0])
                {
                    this.txtRegInfo.AppendText("\r\n" + string.Format("来自{0}:{1}", remote, recStr));
                    string username = hostIP.Replace('.', 'a') + "b" + hostPort;
                    string userKe   = USERe + "a" + USERn;
                    string message  = string.Empty;
                    string str      = rsaDeByKe(recStrArr[1]);
                    // this.txtRegInfo.AppendText("\r\n----------" + str.Split('c')[0]+"|");
                    // this.txtRegInfo.AppendText("\r\n----------" + str.Split('c')[1]+"|");
                    if ("exist" == str)
                    {
                        message = "\r\n用户:" + username + " 已经取得证书。。。";
                    }
                    else if (username == str.Split('c')[0] && userKe == str.Split('c')[1])
                    {
                        //获得证书成功
                        message = "\r\n从服务器取得证书:< " + username + " , " + userKe + " >";
                        this.btnGetUserKey.Enabled = false;
                    }

                    if (message != string.Empty)
                    {
                        this.txtRegInfo.AppendText(message);
                    }
                }


                //广播取得证书用户
                if ("4" == recStrArr[0])
                {
                    //刷新当前用户数组
                    this.cerClients.Clear();
                    for (int i = 1; i < recStrArr.Length; i++)
                    {
                        this.cerClients.Add(recStrArr[i]);
                    }

                    //页面显示当前所有取得证书的用户
                    this.lbxOnlineClient.Items.Clear();
                    try
                    {
                        this.lbxOnlineClient.BeginUpdate();
                        for (int i = 0; i < cerClients.Count; i++)
                        {
                            this.lbxOnlineClient.Items.Add(cerClients[i].ToString());
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.ToString());
                    }
                    finally {
                        this.lbxOnlineClient.EndUpdate();
                    }
                    //Thread.Sleep(200);
                    //this.txtComStatus.AppendText("刷新在线用户。。。\r\n");
                }

                //会话请求返回报文
                if ("5" == recStrArr[0])
                {
                    string str     = rsaDeByKe(recStrArr[1]);
                    string message = string.Empty;


                    //this.txtInput.AppendText("\r\n" + str);
                    if ("exist" == str)
                    {
                        message = "\r\n用户:此通信组已经取得会话密钥。。。";
                    }
                    else
                    {
                        //获得通信组
                        string strRN = rsaDeByKd(str.Replace('d', '$'));

                        string hostUsername = hostIP.Replace('.', 'a') + "b" + hostPort;
                        string comUsername  = null;
                        string rn           = null;
                        string ticks        = null;


                        string[] msg = strRN.Split('c');

                        if (msg[0] != hostUsername)
                        {
                            comUsername = msg[0];
                        }
                        else
                        {
                            comUsername = msg[1];
                        }

                        rn = msg[2];

                        ticks = msg[3];

                        ComClient comClient = new ComClient(comUsername, rn, ticks);

                        comClients.Add(comClient);

                        //页面显示
                        this.lbxComClient.Items.Clear();
                        for (int i = 0; i < comClients.Count; i++)
                        {
                            this.lbxComClient.Items.Add(((ComClient)comClients[i]));
                        }



                        message = "\r\n服务器返回报文:" + strRN;
                    }


                    if (message != string.Empty)
                    {
                        this.txtComStatus.AppendText(message);
                    }
                }

                //会话取消返回报文
                if ("6" == recStrArr[0])
                {
                    this.txtComStatus.AppendText("\r\n取消会话:" + recStrArr[1]);
                    int i = 0;

                    int len = comClients.Count;
                    if (comClients.Count != 0)
                    {
                        for (i = 0; i < len; i++)
                        {
                            if (recStrArr[1] == ((ComClient)comClients[i]).Username)
                            {
                                //删除此会话
                                comClients.RemoveAt(i);
                                break;
                            }
                        }

                        if (i >= len)
                        {
                            //已经没有此会话了
                        }
                        else
                        {
                            //页面显示
                            this.lbxComClient.Items.Clear();
                            for (int j = 0; j < comClients.Count; j++)
                            {
                                this.lbxComClient.Items.Add(((ComClient)comClients[j]));
                            }
                        }
                    }
                }



                //接受通信消息
                if ("7" == recStrArr[0])
                {
                    //源地址 用户名
                    //string username = recStrArr[1];
                    //主机名称
                    string username = hostIP.Replace('.', 'a') + "b" + hostPort;

                    int i = 0;
                    for (i = 0; i < comClients.Count; i++)
                    {
                        if (recStrArr[1] == ((ComClient)comClients[i]).Username)
                        {
                            break;
                        }
                    }

                    if (i >= comClients.Count)
                    {
                        //已经没有此会话了
                    }
                    else
                    {
                        this.txtShowMessage.AppendText("\r\n" + recStrArr[1] + "—>" + username + "  " + TimeUtil.getNowTime() + "\r\n    " + new AES(((ComClient)comClients[i]).Kaes).AesDecipher(recStrArr[2]).Trim());
                    }

                    //  this.txtShowMessage.AppendText("\r\n" +recStrArr[1]+" "+TimeUtil.getNowTime()+"\r\n       "+recStrArr[2]);
                }


                //用户断开消息
                if ("8" == recStrArr[0])
                {
                    string username = rsa.Decryption(recStrArr[1], KDCe, KDCn);

                    // this.txtComStatus.AppendText("\r\n-----断开:" + username);


                    int i = 0;
                    for (i = 0; i < cerClients.Count; i++)
                    {
                        if (cerClients[i].ToString() == username)
                        {
                            cerClients.RemoveAt(i);
                            break;
                        }
                    }



                    i = 0;
                    for (i = 0; i < comClients.Count; i++)
                    {
                        if (((ComClient)comClients[i]).Username == username)
                        {
                            comClients.RemoveAt(i);
                            break;
                        }
                    }



                    //页面显示
                    this.lbxComClient.Items.Clear();
                    for (int j = 0; j < comClients.Count; j++)
                    {
                        this.lbxComClient.Items.Add(((ComClient)comClients[j]));
                    }


                    this.lbxOnlineClient.Items.Clear();
                    for (i = 0; i < cerClients.Count; i++)
                    {
                        this.lbxOnlineClient.Items.Add(cerClients[i].ToString());
                    }



                    this.txtComStatus.AppendText("\r\n----" + username + "断开连接");
                }



                if ("9" == recStrArr[0])
                {
                    string command = rsa.Decryption(recStrArr[1], KDCe, KDCn);

                    if ("disconnect" == command)
                    {
                        //服务器断开 清空所有用户

                        cerClients.Clear();
                        comClients.Clear();

                        this.lbxComClient.Items.Clear();
                        this.lbxOnlineClient.Items.Clear();

                        this.txtComStatus.AppendText("\r\n服务器断开。。。。");
                    }
                }
            }
        }