473,320 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Extra spaces after decryption

I tried the example from the RijndaelManaged class in
http://msdn.microsoft.com/library/de...classtopic.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?

Thank you.
Nov 17 '05 #1
3 2135
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" <Ca**********@discussions.microsoft.com> schrieb im
Newsbeitrag news:DC**********************************@microsof t.com...
I tried the example from the RijndaelManaged class in
http://msdn.microsoft.com/library/de...classtopic.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?

Thank you.

Nov 17 '05 #2
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
Nov 17 '05 #3
Thank you, Jon, your reply was really useful.

You saved me a lot of time and some headaches.

"Jon Skeet [C# MVP]" wrote:
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

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ragnorack67 | last post by:
Hi, I need some advice on what to do. I have this MySQL insert file, with about 30,000 records. One of the datafields has names in it. When this list was put together, apparently there was extra...
5
by: Marco Gallo | last post by:
I've been trying to get rid of extra spaces in a table that I created with addresses in it. For instance in a field called TEST, I got the following address: " 1 main st Apt A "...
0
by: ChrisM | last post by:
Hi, Got a strange one here. I have a report which is part of my dotnet application. It prints fine except for one thing: It adds extra spaces to certain things. eg, required text: 'Service...
5
by: dw | last post by:
hello - first, let me state that i am an Asp.Net rookie. here is the situation: i have a page that looks good in the vs.net designer, but when the page renders there are extra amounts...
7
by: Bosconian | last post by:
I know that str.replace(/^\s+|\s+$/g,''); will trim a string of space, but what about removing extra spaces from the middle? Where "hello world"
3
by: dchristjohn | last post by:
I am currently developing a small windows application using Visual Basic via Visual Studio 2005. My database resides on a SQL 2000 server. I have a table with three fields: id (int, Not Null)...
2
code green
by: code green | last post by:
I am trying to write a simple function that will take a string containing an address line or business name and return it nicely formatted. By this I mean extra spaces removed and words capitalised....
13
by: Tom Andrecht | last post by:
I'm trying to get some encryption/decryption routines going to take care of my data, and while the encryption is working great, I keep running into the message "Padding is invalid and cannot be...
9
by: Betikci Boris | last post by:
I get bored last night and wrote a script that uses xor for encrypt- decrypt, however it woks fine under linux 2.6.25, text and documents are ok, but fails on compressed files *.jpg, *.pdf , etc ....
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.