private void enviarArchivo(String filename, int id_user) { String p; long sz; //IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5656); IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse(host), puerto); Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); sock.Connect(ipEnd); String filedirectory = "envios/"; String filenamekey = "clave.crypt"; FileEncryption fe = new FileEncryption(); fe.makeKey(); fe.saveKey("claves/" + filenamekey, "public.pem"); //tiempo de cifrado del archivo var stopwatch = new Stopwatch(); stopwatch.Start(); //DateTime tiempo5 = DateTime.Now; //Console.WriteLine("INICIO tiempo: " + tiempo5.Ticks); fe.EncryptFile(filedirectory + "planos-medianos/" + filename, filedirectory + "cifrados/" + filename); //tiempo de finalizacion del cifrado archivo stopwatch.Stop(); double tECrip = stopwatch.ElapsedMilliseconds; //DateTime tiempo6 = DateTime.Now; // Console.WriteLine("FIN tiempo: " + tiempo6.Ticks); //double tECrip = (tiempo6 - tiempo5).TotalMilliseconds; //TimeSpan tECrip = new TimeSpan(tiempo6.Ticks - tiempo5.Ticks); Console.WriteLine("TIEMPO DE CIFRADO DE ARCHIVO: {0} ms", tECrip); // Console.WriteLine("TIEMPO DE CIFRADO DE ARCHIVO: {0} ms", tECrip.TotalMilliseconds); sock.Send(Encoding.ASCII.GetBytes(filenamekey)); int bytesReceived = 1; var buffer = new byte[8192]; do { bytesReceived = sock.Receive(buffer); } while (!(bytesReceived > 0)); String ack = Encoding.UTF8.GetString(buffer, 0, bytesReceived); Console.WriteLine("Recibido: " + ack); FileStream fileStream = new FileStream("claves/" + filenamekey, FileMode.Open); byte[] buff = new byte[1024]; int bytesRead = 0; //envio de la clave var stopwatch1 = new Stopwatch(); stopwatch1.Start(); //DateTime tiempo1 = DateTime.Now; //Console.WriteLine("INICIO tiempo: " + tiempo1.Ticks); do { bytesRead = fileStream.Read(buff, 0, buff.Length); if (bytesRead > 0) { sock.Send(buff, bytesRead, SocketFlags.None); } } while (bytesRead > 0); fileStream.Close(); //tiempo de finalizacion de envio clave stopwatch1.Stop(); //DateTime tiempo2 = DateTime.Now; //Console.WriteLine("INICIO fin: " + tiempo2.Ticks); double tEClave = stopwatch1.ElapsedMilliseconds; Console.WriteLine("TIEMPO DE ENVIO DE CLAVE: {0} ms", tEClave); // TimeSpan tEClave = new TimeSpan(tiempo2.Ticks - tiempo1.Ticks); // Console.WriteLine("TIEMPO DE ENVIO DE CLAVE: {0} ms", tEClave.TotalMilliseconds); //TimeSpan tEClave = (tiempo2.TimeOfDay); //TimeSpan tEClave1 = (tiempo1.TimeOfDay); //Console.WriteLine("TIEMPO DE ENVIO DE CLAVE: {0:fff} ", (tEClave.TotalMilliseconds - tEClave1.TotalMilliseconds) + " milisegundos"); //Recibe confirmación buff = new byte[1024]; bytesReceived = 1; //confirmacion de recepcion de la clave do { bytesReceived = sock.Receive(buff); } while (!(bytesReceived > 0) && bytesReceived > buff.Length); ack = Encoding.UTF8.GetString(buff, 0, bytesReceived); Console.WriteLine("Recibido clave: " + ack); //que es lo que va ha realizar insert update o descargar //1 insertar //2 Modificar //3 ver int id = 1; sock.Send(Encoding.ASCII.GetBytes(id.ToString())); Console.WriteLine("Insertar "); //envia nombre del archivo //---------------------------------------------------------------------------- bytesReceived = 1; buffer = new byte[8192]; do { bytesReceived = sock.Receive(buffer); } while (!(bytesReceived > 0)); ack = Encoding.UTF8.GetString(buffer, 0, bytesReceived); Console.WriteLine("Recibido: " + ack); //proceso envio de archivo cifrado sock.Send(Encoding.ASCII.GetBytes(filename)); bytesReceived = 1; buffer = new byte[8192]; do { bytesReceived = sock.Receive(buffer); } while (!(bytesReceived > 0)); ack = Encoding.UTF8.GetString(buffer, 0, bytesReceived); Console.WriteLine("Recibido: " + ack); fileStream = new FileStream(filedirectory + "cifrados/" + filename, FileMode.Open); sock.Send(Encoding.ASCII.GetBytes(fileStream.Length.ToString())); //Envio de tamano do { bytesReceived = sock.Receive(buffer); } while (!(bytesReceived > 0)); ack = Encoding.UTF8.GetString(buffer, 0, bytesReceived); Console.WriteLine("Recibido Tamaño:" + ack); //fileStream = new FileStream(filedirectory + "cifrados/" + filename, FileMode.Open); sz = fileStream.Length; buff = new byte[8192]; bytesRead = 0; var stopwatch2 = new Stopwatch(); stopwatch2.Start(); // DateTime tiempo3 = DateTime.Now; //Console.WriteLine("INICIO tiempo: " + tiempo3.Ticks); //envio de criptograma do { bytesRead = fileStream.Read(buff, 0, buff.Length); if (bytesRead > 0) { //sock.Send(buff); sock.Send(buff, bytesRead, SocketFlags.None); } } while (bytesRead > 0); fileStream.Close(); bytesReceived = 1; buffer = new byte[8192]; do { bytesReceived = sock.Receive(buffer); } while (!(bytesReceived > 0)); ack = Encoding.UTF8.GetString(buffer, 0, bytesReceived); // long tiempo4 = Convert.ToInt64(ack); stopwatch2.Stop(); //DateTime tiempo4 = DateTime.Now; //Console.WriteLine("FIN tiempo: " + tiempo4.Ticks); //TimeSpan tCifradoClave = new TimeSpan(tiempo4.Ticks - tiempo3.Ticks); double tCifradoClave = stopwatch2.ElapsedMilliseconds; Console.WriteLine("TIEMPO DE ENVIO DE CRIPTOGRAMA: {0} ms", tCifradoClave); //Console.WriteLine("TIEMPO DE ENVIO DE CRIPTOGRAMA: {0} ms", tCifradoClave.TotalMilliseconds); //TimeSpan tCifradoClave = (tiempo4.TimeOfDay); //TimeSpan tCifradoClave1 = (tiempo3.TimeOfDay); //Console.WriteLine("TIEMPO DE ENVIO DE CRIPTOGRAMA: {0:fff} " + (tCifradoClave.TotalMilliseconds - tCifradoClave1.TotalMilliseconds) + " milisegundos"); bytesReceived = 1; buffer = new byte[8192]; do { bytesReceived = sock.Receive(buffer); } while (!(bytesReceived > 0)); ack = Encoding.UTF8.GetString(buffer, 0, bytesReceived); cont++; Console.WriteLine("Recibido " + cont + ": " + ack + " ms"); //-------------------------------------------------------------------------------- //envia id Del Usuario Q Inserta Console.WriteLine("Envio Id Usuario:" + id_user); sock.Send(Encoding.ASCII.GetBytes(id_user.ToString())); //Envio de usuario q Inserta //repuesta do { bytesReceived = sock.Receive(buffer); } while (!(bytesReceived > 0)); ack = Encoding.UTF8.GetString(buffer, 0, bytesReceived); Console.WriteLine("DB Menssage:" + ack); //fin envia Id Usuario //-------------------------------------------------------------------------------- sock.Close(); sock = null; Console.WriteLine("Proceso concluido"); //Concatenar variables de tiempos para guardar en un string // p = (cont + "," + sz + "," + tEClave + "," + tCifradoClave + "," + tECrip + "," + ack); //Escribimos en el archivo con el metodo write //guardar.WriteLine(p); // guardar.Flush(); // escribir.close(); //carga DB CargarDatos(); progresSpinnerLoad.Visible = false; MessageBox.Show("Ingreso Correcto", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); }
public void descargarArchivo(String filename, string ubicacionSave) { String p; long sz; //IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5656); IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse(host), puerto); Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); sock.Connect(ipEnd); String filedirectory = "envios/"; String filenamekey = "clave.crypt"; FileEncryption fe = new FileEncryption(); fe.makeKey(); fe.saveKey("claves/" + filenamekey, "public.pem"); //tiempo de cifrado del archivo var stopwatch = new Stopwatch(); stopwatch.Start(); //DateTime tiempo5 = DateTime.Now; //Console.WriteLine("INICIO tiempo: " + tiempo5.Ticks); fe.EncryptFile(filedirectory + "planos-medianos/" + filename, filedirectory + "cifrados/" + filename); //tiempo de finalizacion del cifrado archivo stopwatch.Stop(); double tECrip = stopwatch.ElapsedMilliseconds; //DateTime tiempo6 = DateTime.Now; // Console.WriteLine("FIN tiempo: " + tiempo6.Ticks); //double tECrip = (tiempo6 - tiempo5).TotalMilliseconds; //TimeSpan tECrip = new TimeSpan(tiempo6.Ticks - tiempo5.Ticks); Console.WriteLine("TIEMPO DE CIFRADO DE ARCHIVO: {0} ms", tECrip); // Console.WriteLine("TIEMPO DE CIFRADO DE ARCHIVO: {0} ms", tECrip.TotalMilliseconds); sock.Send(Encoding.ASCII.GetBytes(filenamekey)); int bytesReceived = 1; var buffer = new byte[8192]; do { bytesReceived = sock.Receive(buffer); } while (!(bytesReceived > 0)); String ack = Encoding.UTF8.GetString(buffer, 0, bytesReceived); Console.WriteLine("Recibido: " + ack); FileStream fileStream = new FileStream("claves/" + filenamekey, FileMode.Open); byte[] buff = new byte[1024]; int bytesRead = 0; //envio de la clave var stopwatch1 = new Stopwatch(); stopwatch1.Start(); //DateTime tiempo1 = DateTime.Now; //Console.WriteLine("INICIO tiempo: " + tiempo1.Ticks); do { bytesRead = fileStream.Read(buff, 0, buff.Length); if (bytesRead > 0) { sock.Send(buff, bytesRead, SocketFlags.None); } } while (bytesRead > 0); fileStream.Close(); //tiempo de finalizacion de envio clave stopwatch1.Stop(); //DateTime tiempo2 = DateTime.Now; //Console.WriteLine("INICIO fin: " + tiempo2.Ticks); double tEClave = stopwatch1.ElapsedMilliseconds; Console.WriteLine("TIEMPO DE ENVIO DE CLAVE: {0} ms", tEClave); // TimeSpan tEClave = new TimeSpan(tiempo2.Ticks - tiempo1.Ticks); // Console.WriteLine("TIEMPO DE ENVIO DE CLAVE: {0} ms", tEClave.TotalMilliseconds); //TimeSpan tEClave = (tiempo2.TimeOfDay); //TimeSpan tEClave1 = (tiempo1.TimeOfDay); //Console.WriteLine("TIEMPO DE ENVIO DE CLAVE: {0:fff} ", (tEClave.TotalMilliseconds - tEClave1.TotalMilliseconds) + " milisegundos"); //Recibe confirmación buff = new byte[1024]; bytesReceived = 1; //confirmacion de recepcion de la clave do { bytesReceived = sock.Receive(buff); } while (!(bytesReceived > 0) && bytesReceived > buff.Length); ack = Encoding.UTF8.GetString(buff, 0, bytesReceived); Console.WriteLine("Recibido clave: " + ack); //que es lo que va ha realizar insert update o descargar //1 insertar //2 Modificar //3 descargar int id = 3; sock.Send(Encoding.ASCII.GetBytes(id.ToString())); //sock.Send(Encoding.ASCII.GetBytes("3")); Console.WriteLine("Descargar.. "); //envia nombre del archivo bytesReceived = 1; buffer = new byte[8192]; do { bytesReceived = sock.Receive(buffer); } while (!(bytesReceived > 0)); ack = Encoding.UTF8.GetString(buffer, 0, bytesReceived); Console.WriteLine("Recibido: " + ack); //---------------------------------------------------------------------------- sock.Send(Encoding.ASCII.GetBytes(filename)); bytesReceived = 1; buffer = new byte[8192]; do { bytesReceived = sock.Receive(buffer); } while (!(bytesReceived > 0)); ack = Encoding.UTF8.GetString(buffer, 0, bytesReceived); Console.WriteLine("Recibido: " + ack); buffer = new byte[8192]; bytesReceived = 1; do { bytesReceived = sock.Receive(buffer); } while (!(bytesReceived > 0) && bytesReceived > buffer.Length); long tamano = Convert.ToInt64(Encoding.UTF8.GetString(buffer, 0, bytesReceived)); Console.WriteLine("Recibido:" + tamano.ToString()); sock.Send(Encoding.ASCII.GetBytes("recibido tamano")); // var output = File.Create("descargados/" + filename); var output = File.Create(ubicacionSave + "/" + filename); Console.WriteLine("Recibiendo criptograma"); buffer = new byte[8192]; int contador = 0; do { bytesRead = 0; //System.Threading.Thread.Sleep(1); //if (client.Available > 0) { bytesRead = sock.Receive(buffer, 0, buffer.Length, SocketFlags.None); output.Write(buffer, 0, bytesRead); //} contador += bytesRead; } while (contador < tamano); output.Close(); Console.WriteLine("Recibido criptograma: " + filename); //descifrar // inicia tiempo de desincrifrado var stopwatch2 = new Stopwatch(); stopwatch2.Start(); DateTime tiempo1 = DateTime.Now; // Console.WriteLine("INICIO tiempo: " + tiempo1.Ticks); sock.Send(Encoding.ASCII.GetBytes(tiempo1.Ticks.ToString())); Console.WriteLine("Descifrando criptograma..."); String filedirectorykey = "claves/"; fe.loadKey(filedirectorykey + filenamekey, "private.pem"); //fe.DecryptFile("descargados/" + "cifrados/" + filename, "descargados/" + "descifrados/" + filename); //finaliza tiempo de descrifrado stopwatch2.Stop(); showNotification("Download\n ", "Descarga Correcta\n" + "Guardado en :\n" + ubicacionSave + "\\" + filename); progresSpinnerLoad.Visible = false; sock.Close(); sock = null; Console.WriteLine("Proceso concluido"); MessageBox.Show("Descarga Correcta", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); }