Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Security > ??Difference Between utf8encoder.GetBytes and Encoding.ASCII.GetBytes

Reply
Thread Tools

??Difference Between utf8encoder.GetBytes and Encoding.ASCII.GetBytes

 
 
Phil C.
Guest
Posts: n/a
 
      02-24-2005
Hi. (Using VB.Net) I have a symmetric encryption key stored as text,
encrytped by DPAPI in my web config that I use a handler
class to decrypt by the DPAPI and pass to the class that does the
encryption/decryption.
The decrypted DPAPI key is a string and needs to be converted to a byte
array for use by the encryption/decryption class. I'm confused as to the
difference using utf8encoder.GetBytes() or Encoding.ASCII.GetBytes() to do
this.

Thanks,

Phil
Boston, MA


 
Reply With Quote
 
 
 
 
Joe Kaplan \(MVP - ADSI\)
Guest
Posts: n/a
 
      02-24-2005
Generally speaking, the different encoding classes will give you an array of
bytes from a string corresponding to how that encoding actually represents a
string. Unicode (UTF16) represents each character as 2 bytes. UTF8 will
use a variable number of bytes for each character, but uses only one for
ASCII characters, so it generally uses much less space to store the same
Unicode data.

ASCII converts characters into a single byte using only 7 bits of each byte.
Since it only supports ASCII characters, it can result in data loss if the
string in question contains non-ASCII characters. It rarely has a use in
..NET crypto since strings are unicode in .NET.

If your encryption key is stored as text, it is probably stored in Base64.
In that case, you probably want to use Convert.FromBase64String to convert
the string key into a byte array.

Joe K.

"Phil C." <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi. (Using VB.Net) I have a symmetric encryption key stored as text,
> encrytped by DPAPI in my web config that I use a handler
> class to decrypt by the DPAPI and pass to the class that does the
> encryption/decryption.
> The decrypted DPAPI key is a string and needs to be converted to a byte
> array for use by the encryption/decryption class. I'm confused as to the
> difference using utf8encoder.GetBytes() or Encoding.ASCII.GetBytes() to do
> this.
>
> Thanks,
>
> Phil
> Boston, MA
>



 
Reply With Quote
 
 
 
 
Phil C.
Guest
Posts: n/a
 
      02-24-2005
Thank you Joe, you saved me a lot of grief.
However, then, what is the difference between UTF8Encoding.GetBytes("text")
and Encoding.Unicode.GetBytes("text)
or the converse
UTF8Encoding.GetString(Byte())
Encoding.Unicode.GetString(Byte())
??

-----------------------------------------------------------------------------------------------------------------------------------------------------
"Joe Kaplan (MVP - ADSI)" <(E-Mail Removed)> wrote
in message news:(E-Mail Removed)...
> Generally speaking, the different encoding classes will give you an array
> of bytes from a string corresponding to how that encoding actually
> represents a string. Unicode (UTF16) represents each character as 2
> bytes. UTF8 will use a variable number of bytes for each character, but
> uses only one for ASCII characters, so it generally uses much less space
> to store the same Unicode data.
>
> ASCII converts characters into a single byte using only 7 bits of each
> byte. Since it only supports ASCII characters, it can result in data loss
> if the string in question contains non-ASCII characters. It rarely has a
> use in .NET crypto since strings are unicode in .NET.
>
> If your encryption key is stored as text, it is probably stored in Base64.
> In that case, you probably want to use Convert.FromBase64String to convert
> the string key into a byte array.
>
> Joe K.
>
> "Phil C." <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hi. (Using VB.Net) I have a symmetric encryption key stored as text,
>> encrytped by DPAPI in my web config that I use a handler
>> class to decrypt by the DPAPI and pass to the class that does the
>> encryption/decryption.
>> The decrypted DPAPI key is a string and needs to be converted to a byte
>> array for use by the encryption/decryption class. I'm confused as to the
>> difference using utf8encoder.GetBytes() or Encoding.ASCII.GetBytes() to
>> do this.
>>
>> Thanks,
>>
>> Phil
>> Boston, MA
>>

>
>



 
Reply With Quote
 
Joe Kaplan \(MVP - ADSI\)
Guest
Posts: n/a
 
      02-24-2005
The easiest thing to do is to write some code to test it and see, but I'll
try to explain too.

UTF8 and Unicode (which is really UTF16 as an encoding) are just two
different ways to create a binary encoding of a unicode string. UTF8 uses a
variable number of bytes for each character (depending on the character) and
UTF16 will use 2 bytes for each character. Since you test string, "test",
is all ASCII characters, the UTF8 version will be 4 bytes and the same as
the ASCII version. The Unicode version will be 8 bytes. To see differences
between ASCII and UTF8, you need to use non-ASCII characters in your test.

The various static/shared properties on the Encoding classes are just
shortcuts to keep you from having to build a new instance of the encoding
class. Generally, it will always be a little faster to just use them:

Encoding.UTF8
Encoding.Unicode

HTH,

Joe K.

"Phil C." <(E-Mail Removed)> wrote in message
news:O1$(E-Mail Removed)...
> Thank you Joe, you saved me a lot of grief.
> However, then, what is the difference between
> UTF8Encoding.GetBytes("text")
> and Encoding.Unicode.GetBytes("text)
> or the converse
> UTF8Encoding.GetString(Byte())
> Encoding.Unicode.GetString(Byte())
> ??
>
> -----------------------------------------------------------------------------------------------------------------------------------------------------
> "Joe Kaplan (MVP - ADSI)" <(E-Mail Removed)> wrote
> in message news:(E-Mail Removed)...
>> Generally speaking, the different encoding classes will give you an array
>> of bytes from a string corresponding to how that encoding actually
>> represents a string. Unicode (UTF16) represents each character as 2
>> bytes. UTF8 will use a variable number of bytes for each character, but
>> uses only one for ASCII characters, so it generally uses much less space
>> to store the same Unicode data.
>>
>> ASCII converts characters into a single byte using only 7 bits of each
>> byte. Since it only supports ASCII characters, it can result in data loss
>> if the string in question contains non-ASCII characters. It rarely has a
>> use in .NET crypto since strings are unicode in .NET.
>>
>> If your encryption key is stored as text, it is probably stored in
>> Base64. In that case, you probably want to use Convert.FromBase64String
>> to convert the string key into a byte array.
>>
>> Joe K.
>>
>> "Phil C." <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> Hi. (Using VB.Net) I have a symmetric encryption key stored as text,
>>> encrytped by DPAPI in my web config that I use a handler
>>> class to decrypt by the DPAPI and pass to the class that does the
>>> encryption/decryption.
>>> The decrypted DPAPI key is a string and needs to be converted to a byte
>>> array for use by the encryption/decryption class. I'm confused as to
>>> the difference using utf8encoder.GetBytes() or Encoding.ASCII.GetBytes()
>>> to do this.
>>>
>>> Thanks,
>>>
>>> Phil
>>> Boston, MA
>>>

>>
>>

>
>



 
Reply With Quote
 
Phil C.
Guest
Posts: n/a
 
      02-24-2005
Thanks Joe,
It's the usual "too many ways of doing something" make things more complex.
"Joe Kaplan (MVP - ADSI)" <(E-Mail Removed)> wrote
in message news:%(E-Mail Removed)...
> The easiest thing to do is to write some code to test it and see, but I'll
> try to explain too.
>
> UTF8 and Unicode (which is really UTF16 as an encoding) are just two
> different ways to create a binary encoding of a unicode string. UTF8 uses
> a variable number of bytes for each character (depending on the character)
> and UTF16 will use 2 bytes for each character. Since you test string,
> "test", is all ASCII characters, the UTF8 version will be 4 bytes and the
> same as the ASCII version. The Unicode version will be 8 bytes. To see
> differences between ASCII and UTF8, you need to use non-ASCII characters
> in your test.
>
> The various static/shared properties on the Encoding classes are just
> shortcuts to keep you from having to build a new instance of the encoding
> class. Generally, it will always be a little faster to just use them:
>
> Encoding.UTF8
> Encoding.Unicode
>
> HTH,
>
> Joe K.
>
> "Phil C." <(E-Mail Removed)> wrote in message
> news:O1$(E-Mail Removed)...
>> Thank you Joe, you saved me a lot of grief.
>> However, then, what is the difference between
>> UTF8Encoding.GetBytes("text")
>> and Encoding.Unicode.GetBytes("text)
>> or the converse
>> UTF8Encoding.GetString(Byte())
>> Encoding.Unicode.GetString(Byte())
>> ??
>>
>> -----------------------------------------------------------------------------------------------------------------------------------------------------
>> "Joe Kaplan (MVP - ADSI)" <(E-Mail Removed)>
>> wrote in message news:(E-Mail Removed)...
>>> Generally speaking, the different encoding classes will give you an
>>> array of bytes from a string corresponding to how that encoding actually
>>> represents a string. Unicode (UTF16) represents each character as 2
>>> bytes. UTF8 will use a variable number of bytes for each character, but
>>> uses only one for ASCII characters, so it generally uses much less space
>>> to store the same Unicode data.
>>>
>>> ASCII converts characters into a single byte using only 7 bits of each
>>> byte. Since it only supports ASCII characters, it can result in data
>>> loss if the string in question contains non-ASCII characters. It rarely
>>> has a use in .NET crypto since strings are unicode in .NET.
>>>
>>> If your encryption key is stored as text, it is probably stored in
>>> Base64. In that case, you probably want to use Convert.FromBase64String
>>> to convert the string key into a byte array.
>>>
>>> Joe K.
>>>
>>> "Phil C." <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> Hi. (Using VB.Net) I have a symmetric encryption key stored as text,
>>>> encrytped by DPAPI in my web config that I use a handler
>>>> class to decrypt by the DPAPI and pass to the class that does the
>>>> encryption/decryption.
>>>> The decrypted DPAPI key is a string and needs to be converted to a byte
>>>> array for use by the encryption/decryption class. I'm confused as to
>>>> the difference using utf8encoder.GetBytes() or
>>>> Encoding.ASCII.GetBytes() to do this.
>>>>
>>>> Thanks,
>>>>
>>>> Phil
>>>> Boston, MA
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 04-15-2011 04:00 AM
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 01-06-2011 05:00 PM
Subnets between core and access/distribution layers & routing between layers J Cisco 0 12-13-2006 08:38 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments