I'm having problems with cryptography and sockets, help

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Alejandro Castañaza (da_castanaza_borrar_at_itelgua.com)
Date: 03/14/05


Date: Mon, 14 Mar 2005 10:48:27 -0600

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: Is this the correct way to send a Bitmap over sockets?
    ... // stream is NetworkStream ... Here you don't have to worry about the buffer, ... What if my Bitmap is larger than 1 MB? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Is this the correct way to send a Bitmap over sockets?
    ... I am trying to get the ReceiveBitmap part working using your example. ... NetworkStream stream = client.GetStream; ... Here you don't have to worry about the buffer, ...
    (microsoft.public.dotnet.languages.csharp)
  • NetworkStream.Read troubles
    ... Need a little help with reading from a network stream. ... So I have to use the NetworkStream object. ... Dim fs As New FileStream ... I never get more than 2 or 3 Kb's of an article before the loop ends because ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Is this the correct way to send a Bitmap over sockets?
    ... I'm satisfied with the receiving parts. ... // stream is NetworkStream ... Here you don't have to worry about the buffer, ...
    (microsoft.public.dotnet.languages.csharp)