Carlos Santos <Ca**********@discussions.microsoft.com> wrote:
I tried the example from the RijndaelManaged class in
http://msdn.microsoft.com/library/de...ary/en-us/cpre
f/html/frlrfsystemsecuritycryptographyrijndaelmanagedclas stopic.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...
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too