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

Ask a Question related to ASP.NET Security, Design and Development.

  1. #1

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

    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. Guest

  2. Similar Questions and Discussions

    1. ASCII bug?
      mysql field.test="St. Patrick’s Day " ------------------------ form.cfm: <cfquery name="getfield" datasource="database"> select field from...
    2. XML encoding difference in FLashplayer 5 and player 6 and 7 when parsing dynamic XML from asp.
      Hi, I need to parse a dynamic created XML file from an asp file into flash, when this XML file has special characters like for example the next...
    3. Printing ASCII to Hex
      Hi, There must be an easier way to convert a basic ascii string to hex. I tried using ord/chr/unpack/sprintf(%x) combinations and just dug my...
    4. using tr and ascii value
      I'm updating a process that reads in text file from a mainframe extract. The script reads and parses out this file and creates html documents from...
    5. Ascii to tif.
      AIX version 4.3.3 computer rs6000/f50 Is there a AIX/UNIX utility that will take an ASCII file and convert it to a TIF file?
  3. #2

    Default Re: ??Difference Between utf8encoder.GetBytes and Encoding.ASCII.GetBytes

    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." <charlestek@rcn.com> wrote in message
    news:u6LpKgpGFHA.2616@tk2msftngp13.phx.gbl...
    > 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\) Guest

  4. #3

    Default Re: ??Difference Between utf8encoder.GetBytes and Encoding.ASCII.GetBytes

    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)" <joseph.e.kaplan@removethis.accenture.com> wrote
    in message news:uyWgMXqGFHA.2616@tk2msftngp13.phx.gbl...
    > 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." <charlestek@rcn.com> wrote in message
    > news:u6LpKgpGFHA.2616@tk2msftngp13.phx.gbl...
    >> 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. Guest

  5. #4

    Default Re: ??Difference Between utf8encoder.GetBytes and Encoding.ASCII.GetBytes

    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." <charlestek@rcn.com> wrote in message
    news:O1$VxNrGFHA.3628@TK2MSFTNGP15.phx.gbl...
    > 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)" <joseph.e.kaplan@removethis.accenture.com> wrote
    > in message news:uyWgMXqGFHA.2616@tk2msftngp13.phx.gbl...
    >> 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." <charlestek@rcn.com> wrote in message
    >> news:u6LpKgpGFHA.2616@tk2msftngp13.phx.gbl...
    >>> 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\) Guest

  6. #5

    Default Re: ??Difference Between utf8encoder.GetBytes and Encoding.ASCII.GetBytes

    Thanks Joe,
    It's the usual "too many ways of doing something" make things more complex.
    "Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@removethis.accenture.com> wrote
    in message news:%23AgR7NsGFHA.4084@TK2MSFTNGP14.phx.gbl...
    > 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." <charlestek@rcn.com> wrote in message
    > news:O1$VxNrGFHA.3628@TK2MSFTNGP15.phx.gbl...
    >> 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)" <joseph.e.kaplan@removethis.accenture.com>
    >> wrote in message news:uyWgMXqGFHA.2616@tk2msftngp13.phx.gbl...
    >>> 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." <charlestek@rcn.com> wrote in message
    >>> news:u6LpKgpGFHA.2616@tk2msftngp13.phx.gbl...
    >>>> 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. Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139