Example #1
0
 private void BTSend_Click(object sender, EventArgs e)
 {
     byte[] sendBytes = Encoding.UTF8.GetBytes(tBmsg.Text.Trim());
     if (tCPClient.SendData(TCPClient.Md5pwd, sendBytes))
     {
         rTBMSG.Text += "已发送>>" + tBmsg.Text.Trim() + "\n";
         tBmsg.Text   = "";
     }
 }
        private void Button_Send_Click(object sender, EventArgs e)
        {
            try
            {
                string wholemessage = "(" + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + ")   " + myname + " - " + this.TextBox_Send.Text;
                string mymessage    = "(" + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + ")   " + "You - " + this.TextBox_Send.Text;

                if (flag1 == 1)
                {
                    byte[] Key1 = Encoding.UTF8.GetBytes(this.TextBox_Key.Text);

                    int dem = 0;
                    for (int i = 0; i < 32; i++)
                    {
                        if (dem < Key1.Length)
                        {
                            Key[i] = Key1[dem++];
                        }
                        if (dem == Key1.Length)
                        {
                            dem = 0;
                        }
                    }

                    using (Aes aes = Aes.Create())
                    {
                        byte[] IV = aes.IV;
                        encrypted = Encrypt_String_Aes(wholemessage, Key, aes.IV);

                        //string cipherText = Encoding.UTF8.GetString(encrypted);
                        string plaintext = Decrypt_String_AES(encrypted, Key, IV);

                        int    count1      = 0;
                        int    count2      = 0;
                        int    length      = IV.Length + encrypted.Length;
                        byte[] byte_length = ConvertInt32ToByteArray(length);

                        byte[] byteSend = new byte[IV.Length + encrypted.Length + byte_length.Length];

                        for (int i = 0; i < IV.Length + encrypted.Length + byte_length.Length; i++)
                        {
                            if (i < byte_length.Length)
                            {
                                byteSend[i] = byte_length[i];
                            }
                            else if (i < byte_length.Length + IV.Length)
                            {
                                byteSend[i] = IV[count1++];
                            }
                            else
                            {
                                byteSend[i] = encrypted[count2++];
                            }
                        }

                        tcpclient.SendData(byteSend);
                    }
                }

                Listbox_Chat.Items.Add(mymessage);

                this.TextBox_Send.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Runtime Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }