private void bt_conectar_Click(object sender, EventArgs e) { int puerto = Int32.Parse(tb_puerto.Text); string nombre = tb_numero.Text; rbt_log.Text = "\nIniciando"; TcpClient cliente = new TcpClient(nombre, puerto); if (cliente.Connected) { rbt_log.AppendText("\nconectado"); var destino = tb_destino.Text; var numero = Int32.Parse(tb_numero.Text); NetworkStream stream = cliente.GetStream(); Byte[] mensaje = System.Text.Encoding.ASCII.GetBytes(tb_destino.Text); // byte[] mensajecifrado = Utilidad_Cifrado.EncryptString(mensaje,ka); using (RijndaelManaged myRijndael = new RijndaelManaged()) { myRijndael.GenerateIV(); Utilidad_Cifrado2.IV = myRijndael.IV; myRijndael.Key = ka; byte[] cifrado = Utilidad_Cifrado2.EncryptStringToBytes(tb_destino.Text, myRijndael.Key, myRijndael.IV); stream.Write(cifrado, 0, mensaje.Length); } rbt_log.AppendText("\n" + tb_destino.Text); rbt_log.AppendText("\n" + ByteArrayToString(mensaje)); } }
private async void bt_escuchar_Click(object sender, EventArgs e) { int puerto = Int32.Parse(tb_puerto.Text); IPAddress direccion = IPAddress.Parse(tb_nombre.Text); server = new TcpListener(direccion, puerto); server.Start(); rbt_log.Text = "Iniciado"; while (true) { Byte[] mensaje = new byte[512]; Byte[] IV = new byte[16]; Byte[] resto = new byte[16]; string obtenido = ""; rbt_log.AppendText("\nEsperando Clientes"); TcpClient cliente = await server.AcceptTcpClientAsync(); MessageBox.Show(cliente.Client.RemoteEndPoint.ToString()); rbt_log.AppendText("\nconectado"); NetworkStream stream = cliente.GetStream(); int i = 0; i = stream.Read(mensaje, 0, mensaje.Length); Array.Copy(mensaje, IV, 16); rbt_log.AppendText(Utilidad_Cifrado.ByteArrayToString(IV)); Array.Copy(mensaje, IV.Length, resto, 0, 16); obtenido = Utilidad_Cifrado2.DecryptStringFromBytes(resto, ka, IV); rbt_log.AppendText("\n" + obtenido); /* * while ((i = stream.Read(mensaje, 0, mensaje.Length)) != 0) * { * rbt_log.AppendText("\n"+Utilidad_Cifrado.ByteArrayToString(mensaje)); * * byte[] mensajedecifrado = Utilidad_Cifrado.DecryptString(mensaje,ka); * * //Translate data bytes to a ASCII STRING * obtenido = System.Text.Encoding.ASCII.GetString(mensajedecifrado, 0, i); * rbt_log.AppendText("Recibido: " + obtenido); * } */ cliente.Close(); } }