Re: I'm having problems with cryptography and sockets, help

From: Nicholas Paldino [.NET/C# MVP] (mvp_at_spam.guard.caspershouse.com)
Date: 03/14/05


Date: Mon, 14 Mar 2005 12:43:18 -0500

Alejandro,

    It looks like you are trying to read too much data. You are issuing a
call to the Read method on the socket class passing in a length of 8192
bytes. If the sender does not send 8192 bytes, the call to Read is going to
block until it gets 8192 bytes.

    There are two ways of handling this. The first is to mark your message
with an identifier which indicates that the message has completed
transmission. This would require you to read byte-by-byte, which is not a
good thing.

    The second is to actually send the length of the message before you send
the message yourself. This way, you can issue a call to the Read method and
know you are not asking for more data than is on the line.

    Hope this helps.

-- 
               - Nicholas Paldino [.NET/C# MVP]
               - mvp@spam.guard.caspershouse.com
"Alejandro Castañaza" <da_castanaza_borrar@itelgua.com> wrote in message 
news:%23KyctTLKFHA.3788@tk2msftngp13.phx.gbl...
> Hi.
>
> I'm writing a program, and I need to send confidential data through the 
> network, so I decided to use encryption, using the 
> System.Security.Cryptography namespace.
>
> I'm using the sockets for the network communications, and the program 
> first does a key exchange, with the asymetric cipher classes, to get a new 
> key for the symmetric cipher.  My problem is, that although I have checked 
> that the two points get to the same key and initialization vector, when 
> the sender sends the data, its ok, but the receiver gets blocked.  I am 
> using blocking sockets, so I am aware that if the socket does not have 
> received data, it blocks until it does.  But I know that it does receive 
> the data, and still it blocks, not the NetworkStream, but the CryptoStream 
> used to decrypt the data.  I have been trying many things, using the 
> StreamReader and StreamWriter like in the documentation examples, but 
> doesn't work.
>
> I need help, please.  It's been 2 full days trying, please help.
>
> Alejandro.
>
>
>
> Here are some lines of the code:
>
> This is the receiver code (where it blocks, in the Read function):
>
> string mensaje;
> NetworkStream stream = clienteTcp.GetStream();
> // Crear el stream criptográfico
> CryptoStream crStream = new 
> CryptoStream(stream,TDES.CreateDecryptor(tdesClave,tdesIV),
> 
> CryptoStreamMode.Read);
> // Búfer de 8Kb
> byte[] datos = new byte[8192];
> int bytes;
> int intentos = 25;
> // Detectar si hay datos
> while(intentos > 0)
> {
>    if (stream.DataAvailable)
>        break;
>    intentos--;
>    System.Threading.Thread.Sleep(200);
> }
> // Si no hay datos salir
> if (!stream.DataAvailable)
>    return String.Empty;
> // Leer mensaje
> bytes = crStream.Read(datos,0,datos.Length);
> // Pasar a string
> mensaje = System.Text.Encoding.Unicode.GetString(datos,0,bytes);
>
>
> This is the sender code:
>
> NetworkStream stream = new NetworkStream(clienteTcp);
> // Crear el stream criptográfico
> CryptoStream crStream = new
> CryptoStream(stream,TDES.CreateEncryptor(tdesClave,tdesIV),
> CryptoStreamMode.Write);
> // Convertir el string a una matriz de bytes
> byte[] datos = System.Text.Encoding.Unicode.GetBytes(mensaje);
> // Transmitir mensaje
> crStream.Write(datos,0,datos.Length);
> crStream.Flush();
>
>
>
>
> 


Relevant Pages

  • Re: Will Socket.BeginRead IAsyncResult.CompletedSynchronously always b
    ... > that I use the callback thread for processing. ... > completed a read operation from the socket it will initiate the next read ... FYI- I am actually interfacing with a NetworkStream not the socket. ... new InverseNetworkStream(Socket socket, Stream outputStream) ...
    (microsoft.public.dotnet.framework)
  • Re: stream associate with socket; will one end block another
    ... TCP is a implicit stream. ... NetworkStream Stream class, however you don't have to use it and can use raw ... read/write methods on the socket. ... Until buffer space is full. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem with application trying to read data from a network stream when the socket is closed
    ... I find that when it comes to socket-based streams, ... Since the socket disconnects, and you call ... I recommend using the NetworkStream, and reading byte by byte yourself, ... The code on the server side is of the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: TcpClient Socket Blocking Problems
    ... > I guess I'm more curious as to WHY the NetworkStream doesn't really ... > honor socket blocking in the sence of waiting until I have all the bytes ... The socket is the communication part only. ... The stream is the content. ...
    (microsoft.public.dotnet.general)
  • Re: Async TCPClients
    ... must work out when using a synchronous loop is that one client should not ... What I wanted to do was to create a BinaryReader around a NetworkStream ... Only one client will use a any given socket at one time. ... just serialize the object and take the Stream's length as the length of the ...
    (microsoft.public.dotnet.general)