Encryption and byte array to string conversion

  • Thread starter Thread starter Daniel Moth
  • Start date Start date
D

Daniel Moth

Hi

Regarding encrypting data I have found 3 implementations:
1. The msdn signature sample
2. The sample in the SAMS book
3. The sample in the Apress book

Although slightly different in their implementation they all have what I
think is a common flow:
They use Encoding.ASCII to do the conversion from bytearray to string and
vice versa. I find that does not work if the string contains characters such
as "é" (French e with accent). Instead the characters show as question marks
in a textbox after conversion. Of course, switching to Encoding.Default
solves the problem.

I cannot believe they all made the oversight so I presume I am missing
something. Can anybody point me in the right direction or confirm that using
Encoding.Default makes sense?

Cheers
Daniel
 
Daniel Moth said:
Regarding encrypting data I have found 3 implementations:
1. The msdn signature sample
2. The sample in the SAMS book
3. The sample in the Apress book

Although slightly different in their implementation they all have what I
think is a common flow:
They use Encoding.ASCII to do the conversion from bytearray to string and
vice versa. I find that does not work if the string contains characters such
as "é" (French e with accent). Instead the characters show as question marks
in a textbox after conversion. Of course, switching to Encoding.Default
solves the problem.

I cannot believe they all made the oversight so I presume I am missing
something. Can anybody point me in the right direction or confirm that using
Encoding.Default makes sense?

Encoding.Default isn't a good idea either, as a change to the locale
would invalidate everything. In fact, using any straight encoding with
encrypted data (which is usually binary) is a bad idea - I'd suggest
Base64 encoding the data instead. If you genuniely have text data, I'd
usually recommend UTF8 as a good encoding to use - it's universally
available, and compact for ASCII values.
 
If you genuinely have text data

I have a string (well a bunch of them) that I need to encrypt (the result
being another string) and then store as part of an XML doc... At future
point, the encoded string from XML file must give me back the original
string (by decrypting)...

So encrypting:
string -> Encoding.Default.GetBytes() -> byteArray which encrypted returns
new byteArray -> ToBase64 ->encryptedString...

Decrypting:
encryptedString -> FromBase64 -> byteArray which decrypted returns new
byteArray ->Encoding.Default.GetString ->originalString...

So I'll take your suggestion and substitue use of Encoding.Default with
Encoding.UTF8...

Cheers
Daniel



Daniel Moth said:
Regarding encrypting data I have found 3 implementations:
1. The msdn signature sample
2. The sample in the SAMS book
3. The sample in the Apress book

Although slightly different in their implementation they all have what I
think is a common flow:
They use Encoding.ASCII to do the conversion from bytearray to string and
vice versa. I find that does not work if the string contains characters such
as "é" (French e with accent). Instead the characters show as question marks
in a textbox after conversion. Of course, switching to Encoding.Default
solves the problem.

I cannot believe they all made the oversight so I presume I am missing
something. Can anybody point me in the right direction or confirm that using
Encoding.Default makes sense?

Encoding.Default isn't a good idea either, as a change to the locale
would invalidate everything. In fact, using any straight encoding with
encrypted data (which is usually binary) is a bad idea - I'd suggest
Base64 encoding the data instead. If you genuniely have text data, I'd
usually recommend UTF8 as a good encoding to use - it's universally
available, and compact for ASCII values.
 
Daniel Moth said:
I have a string (well a bunch of them) that I need to encrypt (the result
being another string) and then store as part of an XML doc... At future
point, the encoded string from XML file must give me back the original
string (by decrypting)...

So encrypting:
string -> Encoding.Default.GetBytes() -> byteArray which encrypted returns
new byteArray -> ToBase64 ->encryptedString...

Decrypting:
encryptedString -> FromBase64 -> byteArray which decrypted returns new
byteArray ->Encoding.Default.GetString ->originalString...

So I'll take your suggestion and substitue use of Encoding.Default with
Encoding.UTF8...

Yup, that's a good idea. Apart from that though, it seems fine. The
main thing is not to try to go from the encrypted byte array to a
string just by using an encoding, which some people do :(
 
Thanks again Jon...

What is worst than some people using an encoding in the end, is people
putting out samples that the rest of us follow with the said flows in them
:-(

Cheers
Daniel
 
Back
Top