Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Compact Framework / March 2004

Tip: Looking for answers? Try searching our database.

Encryption and byte array to string conversion

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Daniel Moth - 02 Mar 2004 19:13 GMT
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
Jon Skeet [C# MVP] - 02 Mar 2004 19:34 GMT
> Regarding encrypting data I have found 3 implementations:
> 1. The msdn signature sample
[quoted text clipped - 12 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Daniel Moth - 02 Mar 2004 20:21 GMT
>>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 <dmoth74@hotmail.com> wrote:
> Regarding encrypting data I have found 3 implementations:
> 1. The msdn signature sample
[quoted text clipped - 6 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jon Skeet [C# MVP] - 03 Mar 2004 08:39 GMT
> >>If you genuinely have text data
>
[quoted text clipped - 13 lines]
> 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 :(

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Daniel Moth - 03 Mar 2004 15:35 GMT
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

> > >>If you genuinely have text data
> >
[quoted text clipped - 17 lines]
> 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 :(
casey chesnut - 03 Mar 2004 19:37 GMT
i think mine uses Encoding.UTF8
http://www.brains-N-brawn.com/spCrypt
casey

> Hi
>
[quoted text clipped - 17 lines]
> Cheers
> Daniel

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.