private void btn_SendeMessage_Click(object sender, EventArgs e) { if (lst_OnlineUser.SelectedIndex != -1) { string wholename = null; string publickeyfromuser = null; for (int i = 0; i < userInfo.Length; i++) { //MessageBox.Show(""+(userInfo[i,0])); if (lst_OnlineUser.SelectedItem.Equals(userInfo[i, 0])) { wholename = userInfo[i, 1]; publickeyfromuser = userInfo[i, 1].Substring(0, 245); break; } } //string publickeyfromuser = lst_OnlineUser.SelectedItem + ""; //publickeyfromuser = publickeyfromuser.Substring(0, 245); string encrypteddata; try { UnicodeEncoding ByteConverter = new UnicodeEncoding(); byte[] encrypteData; byte[] plainttext; plainttext = ByteConverter.GetBytes(rtf_SendMessage.Text); RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); RSA.FromXmlString(publickeyfromuser); encrypteData = RSAEnCrypto(plainttext, RSA.ExportParameters(false), false); encrypteddata = Convert.ToBase64String(encrypteData); } catch (Exception f) { Console.WriteLine(f); encrypteddata = null; } /* * abc = rtf_SendMessage.Text; * string aaa = SRSA.Encrypt(abc); */ string bbb = SRSA.Decrypt(encrypteddata); AsyncSendMessage("Talk," + wholename + "," + encrypteddata + "\r\n"); AddTalkMessage(txt_UserName.Text + " to " + lst_OnlineUser.SelectedItem + ":\r\n"); AddTalkMessage(rtf_SendMessage.Text + "\n"); if (rtf_SendMessage.Text.Equals("Bye")) { Application.Exit(); } rtf_SendMessage.Clear(); //AddTalkMessage(splitString[1].Substring(245) + " to " + txt_UserName.Text + ":\r\n"); //string finalmessage = SRSA.Decrypt(splitString[2]); //AddTalkMessage(finalmessage + "\n"); } else { MessageBox.Show("请先在[当前在线]中选择一个对话者"); } }
/// <summary> /// 处理接收的服务器收据 /// </summary> private void ReceiveData() { string receiveString = null; while (!isExit) { ReceiveMessageDelegate d = new ReceiveMessageDelegate(receiveMessage); IAsyncResult result = d.BeginInvoke(out receiveString, null, null); //使用轮询方式来盘点异步操作是否完成 while (!result.IsCompleted) { if (isExit) { break; } Thread.Sleep(250); } //获取Begin方法的返回值所有输入/输出参数 d.EndInvoke(out receiveString, result); if (receiveString == null) { if (!isExit) { MessageBox.Show("与服务器失去联系"); } break; } string[] splitString = receiveString.Split(','); string command = splitString[0]; switch (command) { case "Login": //格式: login,用户名 for (int i = 0; i < userInfo.Length; i++) { if (userInfo[i, 0] != null) { continue; } else { userInfo[i, 0] = splitString[1].Substring(245); userInfo[i, 1] = splitString[1]; AddOnline(userInfo[i, 0]); //MessageBox.Show("dan yonghuming: "+userInfo[i, 0] + "\n wanzhengmingzi: " + userInfo[i, 1]); break; //rtf_SendMessage.Text = userInfo[i, 0] + "\n" + userInfo[i, 1]; } } //AddOnline(splitString[1]); break; case "Logout": //格式: logout,用户名 RemoveUserName(splitString[1].Substring(245)); break; case "Talk": //格式: talk,用户名,对话信息 AddTalkMessage(splitString[1].Substring(245) + " to " + txt_UserName.Text + ":\r\n"); string finalmessage = SRSA.Decrypt(splitString[2]); AddTalkMessage(finalmessage + "\n"); //AddTalkMessage(receiveString.Substring(splitString[0].Length + splitString[1].Length + 2)); break; } } Application.Exit(); }