Replace ASCII (null) value in string (RijndaelManaged)

D

daz_oldham

Hi

I am using the RijndaelManaged encryption method in C#, but when I
decode my string, the byte array is returning several ASCII null values
at the end of my string, which is represented by "\0".

What I would like to know, is how can I get rid of these null values as
obviously I don't want or need them.

Many thanks

Darren

// Code snippet
return Encoding.ASCII.GetString(bytTmp);
 
C

cody

daz_oldham said:
Hi

I am using the RijndaelManaged encryption method in C#, but when I
decode my string, the byte array is returning several ASCII null values
at the end of my string, which is represented by "\0".

What I would like to know, is how can I get rid of these null values as
obviously I don't want or need them.

Many thanks

Darren

// Code snippet
return Encoding.ASCII.GetString(bytTmp);

try to use base64 encoding on the array to retrieve a ascii string.
 
J

Jon Skeet [C# MVP]

daz_oldham said:
I am using the RijndaelManaged encryption method in C#, but when I
decode my string, the byte array is returning several ASCII null values
at the end of my string, which is represented by "\0".

What I would like to know, is how can I get rid of these null values as
obviously I don't want or need them.

You're treating arbitrary binary data as if it were text - it's not.
Use Convert.ToBase64String instead.
 
D

daz_oldham

Hi Jon, Cody

When I try to do this, I am getting the following back ( my string =
"9").

"QAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

The encryption clearly uses ASCII encoding, would I be better using
Unicode?

Many thanks

Darren
 
J

Jon Skeet [C# MVP]

daz_oldham said:
When I try to do this, I am getting the following back ( my string =
"9").

"QAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

Yup - that's fairly reasonable.
The encryption clearly uses ASCII encoding, would I be better using
Unicode?

No. Encryption doesn't use ASCII encoding - it converts arbitrary
binary data to arbitrary binary data. Base64 is a safe way of
representing arbitrary binary data as text.

Jon
 
D

daz_oldham

Ah, thanks Jon.

I did a search on RijndaelManaged c# and found loads of implementations
of this, and used another implementation of it - it has done the trick
so I am happy.

Thanks for your help.

Darren
 

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

Top