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

Discussion in 'ASP .Net Security' started by Phil C., Feb 24, 2005.

  1. Phil C.

    Phil C. Guest

    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
     
    Phil C., Feb 24, 2005
    #1
    1. Advertising

  2. 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." <> wrote in message
    news:...
    > 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
    >
     
    Joe Kaplan \(MVP - ADSI\), Feb 24, 2005
    #2
    1. Advertising

  3. Phil C.

    Phil C. Guest

    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)" <> wrote
    in message news:...
    > 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." <> wrote in message
    > news:...
    >> 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
    >>

    >
    >
     
    Phil C., Feb 24, 2005
    #3
  4. 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." <> wrote in message
    news:O1$...
    > 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)" <> wrote
    > in message news:...
    >> 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." <> wrote in message
    >> news:...
    >>> 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
    >>>

    >>
    >>

    >
    >
     
    Joe Kaplan \(MVP - ADSI\), Feb 24, 2005
    #4
  5. Phil C.

    Phil C. Guest

    Thanks Joe,
    It's the usual "too many ways of doing something" make things more complex.
    "Joe Kaplan (MVP - ADSI)" <> wrote
    in message news:%...
    > 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." <> wrote in message
    > news:O1$...
    >> 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)" <>
    >> wrote in message news:...
    >>> 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." <> wrote in message
    >>> news:...
    >>>> 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
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    Phil C., Feb 24, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tiraman
    Replies:
    0
    Views:
    833
    Tiraman
    May 10, 2004
  2. jakk
    Replies:
    4
    Views:
    12,511
  3. Andrew
    Replies:
    5
    Views:
    7,023
    John C. Bollinger
    Sep 28, 2004
  4. Replies:
    2
    Views:
    923
  5. Rui
    Replies:
    1
    Views:
    620
    Marc 'BlackJack' Rintsch
    Sep 22, 2008
Loading...

Share This Page