Extra spaces after decryption

  • Thread starter Thread starter Guest
  • Start date Start date
Hi Carlos,

the Rijndael-algorithm is a blockcipher-algorithm.
i suppose, the size of the original text is not a whole number of blocks, so
it's padded to the apropriate size.
 
Carlos Santos said:
I tried the example from the RijndaelManaged class in
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpre
f/html/frlrfsystemsecuritycryptographyrijndaelmanagedclasstopic.asp
and I noticed that the result of the roundtrip has some extra spaces, so
that the result isn't equal to the original text.

Can someone tell me why this is happening and how can it be corrected?

The problem is the way that the decrypted data is being read. This
line:

fromEncrypt = new byte[encrypted.Length];

assumes that the length of the decrypted data is the same as the
encrypted data. The return value of Read is then ignored.

Changing the following lines to:

//Read the data out of the crypto stream.
int bytes = csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

//Convert the byte array back into a string.
roundtrip = textConverter.GetString(fromEncrypt, 0, bytes);

will do the job in this particular case, but it would be better to use:

using (StreamReader reader = new StreamReader (csDecrypt,
textConverter))
{
roundtrip = reader.ReadToEnd();
}

Frankly the code is pretty badly written - declaring variables at the
top, creating a new ASCIIEncoding rather than using Encoding.ASCII, not
closing any streams etc...
 
Thank you, Jon, your reply was really useful.

You saved me a lot of time and some headaches.



Jon Skeet said:
Carlos Santos said:
I tried the example from the RijndaelManaged class in
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpre
f/html/frlrfsystemsecuritycryptographyrijndaelmanagedclasstopic.asp
and I noticed that the result of the roundtrip has some extra spaces, so
that the result isn't equal to the original text.

Can someone tell me why this is happening and how can it be corrected?

The problem is the way that the decrypted data is being read. This
line:

fromEncrypt = new byte[encrypted.Length];

assumes that the length of the decrypted data is the same as the
encrypted data. The return value of Read is then ignored.

Changing the following lines to:

//Read the data out of the crypto stream.
int bytes = csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

//Convert the byte array back into a string.
roundtrip = textConverter.GetString(fromEncrypt, 0, bytes);

will do the job in this particular case, but it would be better to use:

using (StreamReader reader = new StreamReader (csDecrypt,
textConverter))
{
roundtrip = reader.ReadToEnd();
}

Frankly the code is pretty badly written - declaring variables at the
top, creating a new ASCIIEncoding rather than using Encoding.ASCII, not
closing any streams etc...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top