Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Meta en 20 Mayo 2009, 21:02 pm



Título: cifrar tramas de bytes
Publicado por: Meta en 20 Mayo 2009, 21:02 pm
Buenas:
He hecho un programa con Visual C# Express 2008. Puedo enviar tramas desde Internet.
Cliente: Introduces un buuton1 y un textBox1.
Código
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Net;
  11. using System.Net.Sockets;
  12.  
  13. namespace Client
  14. {
  15.    public partial class Form1 : Form
  16.    {
  17.        public Form1()
  18.        {
  19.            InitializeComponent();
  20.        }
  21.  
  22.        private void button1_Click(object sender, EventArgs e)
  23.        {
  24.            UdpClient udpClient = new UdpClient();
  25.            udpClient.Connect(textBox1.Text, 60000);
  26.            Byte[] sendBytes = Encoding.ASCII.GetBytes("Hola a todo el mundo...");
  27.            udpClient.Send(sendBytes, sendBytes.Length);
  28.        }
  29.    }
  30. }
  31.  

Server:
Introduces un listBox1.
Código
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10. using System.Net.Sockets;
  11. using System.Net;
  12.  
  13. namespace Server
  14. {
  15.    public partial class Form1 : Form
  16.    {
  17.        public Form1()
  18.        {
  19.            InitializeComponent();
  20.        }
  21.  
  22.        public void serverThread()
  23.        {
  24.            UdpClient udpClient = new UdpClient(60000);
  25.            while(true)
  26.            {
  27.                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
  28.                Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
  29.                string returnData = Encoding.ASCII.GetString(receiveBytes);
  30.                 listBox1.Items.Add(RemoteIpEndPoint.Address.ToString() +
  31.                    ":" + returnData.ToString());
  32.            }
  33.        }
  34.  
  35.        private void Form1_Load(object sender, EventArgs e)
  36.        {
  37.            Thread thdUDPServer = new Thread(new
  38.            ThreadStart(serverThread));
  39.            thdUDPServer.Start();
  40.        }
  41.    }
  42. }
  43.  

Mi idea es que necesito encriptrar estas tramas que se envía a través  de Internet para que los sniffer (husmeadores) no cojan libremente los datos enviados. Los datos puedes ser textos de un chat.

He encontrado algo aquí, pero no entiendo nada.
http://msdn.microsoft.com/es-es/library/system.security.cryptography.des.aspx

Código
  1. private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
  2. {    
  3.     //Create the file streams to handle the input and output files.
  4.     FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
  5.     FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
  6.     fout.SetLength(0);
  7.  
  8.     //Create variables to help with read and write.
  9.     byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
  10.     long rdlen = 0;              //This is the total number of bytes written.
  11.     long totlen = fin.Length;    //This is the total length of the input file.
  12.     int len;                     //This is the number of bytes to be written at a time.
  13.  
  14.     DES des = new DESCryptoServiceProvider();          
  15.     CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
  16.  
  17.     Console.WriteLine("Encrypting...");
  18.  
  19.     //Read from the input file, then encrypt and write to the output file.
  20.     while(rdlen < totlen)
  21.     {
  22.         len = fin.Read(bin, 0, 100);
  23.         encStream.Write(bin, 0, len);
  24.         rdlen = rdlen + len;
  25.         Console.WriteLine("{0} bytes processed", rdlen);
  26.     }
  27.  
  28.     encStream.Close();  
  29.     fout.Close();
  30.     fin.Close();                  
  31. }
  32.  

http://msdn.microsoft.com/es-es/library/system.security.cryptography.aspx


Título: Re: cifrar tramas de bytes
Publicado por: MazarD en 20 Mayo 2009, 21:40 pm
Qué es exactamente lo que no entiendes?

Para empezar es bueno tener una visión general de la cifrado que vas a utilizar, en el caso de DES es bastante importante:
http://es.wikipedia.org/wiki/Data_Encryption_Standard

Luego la función que has pegado simplemente recibe ruta de entrada, ruta de salida, clave y te genera el archivo cifrado, puedes probarlo llamandola así por ejemplo:

Código
  1.            byte[] llave= new byte[8]{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11};
  2.            byte[] iv= new byte[8] {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11};
  3.            EncryptData("c:\\archivoAencriptar.txt", "c:\\archivoAgenerar.txt", llave, iv);
  4.  

:P

Para descifrar, en la función de ejemplo que tienes, deberías cambiar createencryptor por createdecryptor y hacer que el stream fuera de lectura.
Bueno si tienes alguna duda concreta, comentalo.

Saludos!