private void ShakeHand() { if (PUB.ActiveC) { PUB.s_ctrl.Send(Encoding.UTF8.GetBytes(pubkey)); counter = PUB.s_ctrl.Receive(bytesReceived, bytesReceived.Length, 0); pubkey4send = Encoding.UTF8.GetString(bytesReceived, 0, counter); AES_module.AES_Initialize(out PUB.AES_Key, out PUB.AES_IV); byte[] AES_Complex = AES_join(PUB.AES_Key, PUB.AES_IV); byte[] cipherkey = RSA_module.RSAEncrypt(AES_Complex, pubkey4send); PUB.s_ctrl.Send(cipherkey); } else { counter = PUB.s_ctrl.Receive(bytesReceived, bytesReceived.Length, 0); pubkey4send = Encoding.UTF8.GetString(bytesReceived, 0, counter); PUB.s_ctrl.Send(Encoding.UTF8.GetBytes(pubkey)); counter = PUB.s_ctrl.Receive(bytesReceived, bytesReceived.Length, 0); byte[] cipherkey = new byte[counter]; Array.Copy(bytesReceived, 0, cipherkey, 0, counter); byte[] AES_Complex = RSA_module.RSADecrypt(cipherkey, privkey); Buffer.BlockCopy(AES_Complex, 0, PUB.AES_Key, 0, 32); Buffer.BlockCopy(AES_Complex, 32, PUB.AES_IV, 0, 16); } }
private void SendMSG_Button_Click(object sender, EventArgs e) { DateTime dt = DateTime.Now; RecvBox.AppendText(dt.ToString() + " " + CFG.UserID + ":" + Environment.NewLine); RecvBox.AppendText(TransBox.Text + Environment.NewLine); byte[] sendcache = AES_module.AES_Encrypt(Encoding.UTF8.GetBytes(TransBox.Text), AES_Key, AES_IV); PUB.s.Send(sendcache); TransBox.Clear(); }
private void textbox_KeyDown(object sender, KeyEventArgs e) { if (e.KeyData == (Keys.Control | Keys.Enter)) { DateTime dt = DateTime.Now; RecvBox.AppendText(dt.ToString() + " " + CFG.UserID + ":" + Environment.NewLine); RecvBox.AppendText(TransBox.Text + Environment.NewLine); byte[] sendcache = AES_module.AES_Encrypt(Encoding.UTF8.GetBytes(TransBox.Text), PUB.AES_Key, PUB.AES_IV); PUB.s_chat.Send(sendcache); TransBox.Clear(); TransBox.Focus(); } }
public void Recv() { Action <string> Show_Recv = new Action <string>(RecvboxFresh); while (true) { tmp = PUB.s.Receive(bytesReceived, bytesReceived.Length, 0); byte[] cipherbyte = new byte[tmp]; Array.Copy(bytesReceived, 0, cipherbyte, 0, tmp); byte[] plainbytes = AES_module.AES_Decrypt(cipherbyte, AES_Key, AES_IV); string plaintext = Encoding.UTF8.GetString(plainbytes); Invoke(Show_Recv, plaintext); } }
private void Recv() { Action <string> Show_Recv = new Action <string>(RecvboxFresh); try { while (true) { counter = PUB.s_chat.Receive(bytesReceived, bytesReceived.Length, 0); byte[] cipherbyte = new byte[counter]; Array.Copy(bytesReceived, 0, cipherbyte, 0, counter); byte[] plainbytes = AES_module.AES_Decrypt(cipherbyte, PUB.AES_Key, PUB.AES_IV); string plaintext = Encoding.UTF8.GetString(plainbytes); Invoke(Show_Recv, plaintext); } } catch (SocketException e) { } }
public Chat_Form() { InitializeComponent(); TargetID_Lable.Text = "正在与 " + CFG.TargetID + " 聊天:"; RSA_module.RSA_pair(out pubkey, out privkey); if (PUB.ActiveC) { PUB.s.Send(Encoding.UTF8.GetBytes(pubkey)); tmp = PUB.s.Receive(bytesReceived, bytesReceived.Length, 0); pubkey4send = Encoding.UTF8.GetString(bytesReceived, 0, tmp); AES_module.AES_Initialize(out AES_Key, out AES_IV); byte[] AES_Complex = AES_join(AES_Key, AES_IV); byte[] cipherkey = RSA_module.RSAEncrypt(AES_Complex, pubkey4send); PUB.s.Send(cipherkey); } else { tmp = PUB.s.Receive(bytesReceived, bytesReceived.Length, 0); pubkey4send = Encoding.UTF8.GetString(bytesReceived, 0, tmp); PUB.s.Send(Encoding.UTF8.GetBytes(pubkey)); tmp = PUB.s.Receive(bytesReceived, bytesReceived.Length, 0); byte[] cipherkey = new byte[tmp]; Array.Copy(bytesReceived, 0, cipherkey, 0, tmp); byte[] AES_Complex = RSA_module.RSADecrypt(cipherkey, privkey); Buffer.BlockCopy(AES_Complex, 0, AES_Key, 0, 32); Buffer.BlockCopy(AES_Complex, 32, AES_IV, 0, 16); } Thread Rx = new Thread(new ThreadStart(Recv)); Rx.Start(); SendMSG_Button.Enabled = true; }