Re: a problem with encryption
From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 08/11/04
- Next message: Alby: "Troubles in installing .NET on W2k laptop"
- Previous message: amey kale: "templates in managed c++"
- In reply to: Tonci Jukic: "Re: a problem with encryption"
- Next in thread: Tonci Jukic: "Re: a problem with encryption"
- Reply: Tonci Jukic: "Re: a problem with encryption"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 11 Aug 2004 08:22:28 +0100
Tonci Jukic <nytrogen@email.htnet.hr> wrote:
> >The problem is that you're assuming the decrypted size will be the same
> as the encrypted size - it's not. You're only actually reading 15 bytes
> (and moreover, you're assuming they'll all be read in one go, which is a
> bad idea) but passing the encoding a buffer 16 bytes long.
> >*Always* use the return value of Stream.Read.
>
> Well, the problem was not in network operations and data send. It was in
> decrypted data.
I didn't say it *was* in the network operations.
> The problem was I did always get n to 16 bytes filled with zeros.
That's because you ignored the fact that Read wasn't returning 16
bytes.
> So I've just swapped line:
>
> roundtrip = textConverter.GetString(fromEncrypt);
>
> with:
>
> roundtrip =
> textConverter.GetString(fromEncrypt).TrimEnd(Convert.ToChar(0));
>
> That way I always get the original data I've encrypted.
That's a bad way of doing things. Just use the return value of Read to
find out how much real data you've got, and use the form of GetString
that lets you specify how much to decode.
> Thanks btw.
>
> I have another question:
> When I'm sending this data trough network stream from client to server,
> I always create byte type array big enough to accept possible data from
> the client application.
> Do I have to always create it big enough to support any possible data
> size, or I can read everything in blocks and then merge it to a single
> byte array for example.
Yes. That's much more robust - relying on a single call to Read as you
are at the moment is a very bad idea.
See http://www.pobox.com/~skeet/csharp/readbinary.html
> This is how it is done by now:
<snip>
> byte[] encrypted = new byte[returned.Length-48];
<snip>
> encrypted.DeCrypt();
That's not your actual code, is it? Byte arrays don't have a DeCrypt
method. Please always post your *actual* code.
-- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
- Next message: Alby: "Troubles in installing .NET on W2k laptop"
- Previous message: amey kale: "templates in managed c++"
- In reply to: Tonci Jukic: "Re: a problem with encryption"
- Next in thread: Tonci Jukic: "Re: a problem with encryption"
- Reply: Tonci Jukic: "Re: a problem with encryption"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|