Re: a problem with encryption

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 08/11/04


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


Relevant Pages

  • Re: Question about bit strength
    ... the encode and the decode methods are exactly the same. ... A repeating usually key weakens the cypher, ... seperate of both the encrypt and decrypt functions. ...
    (sci.crypt)
  • Re: a problem with encryption
    ... >> That way I always get the original data I've encrypted. ... I really don't know a way to know how long the string I send to ... > encrypted data in a byte array trough network stream. ... to decrypt in one call, ...
    (microsoft.public.dotnet.general)
  • Re: Question about bit strength
    ... ever going to give you the same sequence when you decrypt. ... generating an 8 character hex, but it is still seeded by the system clock, i ... simple block cypher or stream cypher to start with. ...
    (sci.crypt)
  • Re: RijndaelManaged decryption leaves strange characters
    ... When you convert the array of decrypted bytes to string, ... how many plain text bytes were returned, only convert these bytes to string. ... > Encrypt the data using RijndaelManaged in CBC Mode. ... > Decrypt using RijndaelManaged in CBC Mode ...
    (microsoft.public.dotnet.security)
  • Re: DES Decrypt Not Working
    ... > to decrypt it, it returns the exact same byte array that I passed to ... > Function DecryptData(ByVal bData() As Byte) As String ...
    (microsoft.public.dotnet.general)

Loading