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); } }
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; }