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

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 03/18/05


Date: Fri, 18 Mar 2005 06:01:22 -0000

Alejandro Castaņaza <da_castanaza_borrar@itelgua.com> wrote:
> I made many changes to the original code after I posted, because I was still
> trying to make it work. Then I added the call to FlushFinalBlock, but the
> Read call still blocked. Then I used the streamReader, ReadToEnd and
> blocked. Then, after looking some example code for encryption, I tried the
> solution I wrote earlier, using the cryptostream object to write to a temp
> memorystream, and reading the plain text from it. Its working that way.
>
> I didn't mean to say for sure it is a bug. Since the first message I asked
> for help, because I thought maybe I was missing something (like the call to
> flushfinalblock) or was doing something wrong.
>
> I havent tried, but after I finally made it work, I thought that the problem
> was because encryption always expand a little the original block of data,
> and that was why I was having trouble with it. That is the reason to call
> FlushFinalBlock or close the cryptostream, right? so it can finish the
> encryption process and send the last block of encrypted data.

Not only that - but so it can then tell the stream that it's finished,
so that things like ReadToEnd will complete. If you leave the stream
open, things like ReadToEnd won't know whether there might be some more
data to come or not.

> So maybe I have to try reading the networkstream using the
> cryptostream with this concept in mind. But I'll do it later. Its
> working now, and I have so much to do still about that program.

Fair enough. If you do find out what was wrong, please report back to
the group :)

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Re: RijndaelManaged and Rijndael CryptoTransforms do not support CFB or OFB CrytoModes
    ... > transform instance the same IV will be used to start off on the first ... In CFB mode, it is *vital* that you rotate IVs as often as possible. ... can induce plain text data into your encryption process himself. ... > message to its respective CryptoStream I will call FlushFinalBlock. ...
    (microsoft.public.dotnet.security)
  • Re: Encrypt a string to a string and vice versa
    ... fine (at least I don't get an exception anywhere, if the string is ... correctly encryption is left aside). ... where I'm just creating a MemoryStream by ... The problem is, as soon as I call ReadByte on the CryptoStream, I get the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: DES and UUEncoded
    ... using DES encryption is easy. ... decryption. ... > Then you hook up a CryptoStream to that memory stream, ... > decrypted output will be avaliable in the memory stream. ...
    (microsoft.public.dotnet.security)
  • Re: Encrypt a string to a string and vice versa
    ... All I want to do is to encrypt a string into an encrypted ... The encryption libraries in .NET are ... Look at CryptoStream for some sample code. ... Convert the resulting binary data into a string again. ...
    (microsoft.public.dotnet.languages.csharp)