title: byte.Parse (c#) : FormatException on Hexadecimal string
body: <p>I'm actually trying to implement a very simple login mecanism for an app I'm developping in Visual C# .NET 2.0 on an embedded device. After some researches, I've found on the msdn a code sample performing password hashing :</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/aa545602(v=cs.70).aspx" rel="nofollow">How to store passwords</a></p>
  
  <p>Unfortunately, when I try to use it, that code sample is raising a FormatException on the call to <code>byte.Parse</code> on the substrings of the hexadecimal string <code>SaltValue</code>. I really have trouble to understand why, since I haven't done any change to the code.</p>
  
  <p>Here is the code :</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Security.Cryptography;
  using System.Globalization;
  
  private const int SaltValueSize = 4;
  
  private static string GenerateSaltValue()
  {
  UnicodeEncoding utf16 = new UnicodeEncoding();
  
  if (utf16 != null)
  {
      // Create a random number object seeded from the value
      // of the last random seed value. This is done
      // interlocked because it is a static value and we want
      // it to roll forward safely.
  
      Random random = new Random(unchecked((int)DateTime.Now.Ticks));
  
      if (random != null)
      {
          // Create an array of random values.
  
          byte[] saltValue = new byte[SaltValueSize];
  
          random.NextBytes(saltValue);
  
          // Convert the salt value to a string. Note that the resulting string
          // will still be an array of binary values and not a printable string. 
          // Also it does not convert each byte to a double byte.
  
  
          //Original line :
          //string saltValueString = utf16.GetString(saltValue);
          //Replaced by :
          string saltValueString = utf16.GetString(saltValue, 0, SaltValueSize);
  
          // Return the salt value as a string.
  
          return saltValueString;
      }
  }
  
  return null;
  }
  
  private static string HashPassword(string clearData, string saltValue, HashAlgorithm hash)
  {
  UnicodeEncoding encoding = new UnicodeEncoding();
  
  if (clearData != null &amp;&amp; hash != null &amp;&amp; encoding != null)
  {
      // If the salt string is null or the length is invalid then
      // create a new valid salt value.
  
      if (saltValue == null)
      {
          // Generate a salt string.
          saltValue = GenerateSaltValue();
      }
  
      // Convert the salt string and the password string to a single
      // array of bytes. Note that the password string is Unicode and
      // therefore may or may not have a zero in every other byte.
  
      byte[] binarySaltValue = new byte[SaltValueSize];
  
      //FormatException raised here
      binarySaltValue[0] = byte.Parse(saltValue.Substring(0, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
      binarySaltValue[1] = byte.Parse(saltValue.Substring(2, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
      binarySaltValue[2] = byte.Parse(saltValue.Substring(4, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
      binarySaltValue[3] = byte.Parse(saltValue.Substring(6, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
  
  //...
  //Some more code
  //...
  }
  }
  </code></pre>
  
  <p>I only have changed one line :</p>
  
  <p><code>string saltValueString = utf16.GetString(saltValue);</code></p>
  
  <p>to</p>
  
  <p><code>string saltValueString = utf16.GetString(saltValue, 0, SaltValueSize);</code></p>
  
  <p>because the first version of the method doesn't seem to be available for embedded C#. But anyway I've tested without changing this line (on a non-embedded environment), and it still was raising a FormatException.</p>
  
  <p>I've copied the <code>SaltValueSize</code> value from that other msdn code sample (which is related) :
  <a href="http://msdn.microsoft.com/en-us/library/aa545760(v=cs.70).aspx" rel="nofollow">How to validate passwords</a></p>
  
  <p>The test that raises the exception :  </p>
  
  <p><code>HashPassword("youpi", null, new SHA1CryptoServiceProvider());</code></p>