バイトになります。Parse(c#):FormatExceptionに進数文字列
-
20-12-2019 - |
質問
いて実施する非常に簡単ログインmecanismのためのアプリっ開発Visual C#.純2.0組み込み装置です。その後の研究がその場で発音を確認することが、msdnのコードサンプルを行うパスワードハッシュ:
残念ながら写真撮影のポイントにご案内していると、そのコードサンプルを向上させることにより、FormatExceptionを呼び出 byte.Parse
の部分文字列が、十六進文字列 SaltValue
.ってトラブルその理由を理解するには、俺には変わります。
こちらのコード:
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 && hash != null && 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
//...
}
}
今回の変更は一線:
string saltValueString = utf16.GetString(saltValue);
へ
string saltValueString = utf16.GetString(saltValue, 0, SaltValueSize);
では、最初のバージョンの方法があるようには見えません利用でき込C#.かん検を変更せずにこのライン(以外の組込み環境が続いていたのを向上させることにより、FormatException.
私はコピーされる SaltValueSize
値からその他のmsdnのコード例である関連):どのように検証するパスワード
の試験の例外:
HashPassword("youpi", null, new SHA1CryptoServiceProvider());
解決
に問題があるこ GenerateSaltValue
メソッドは文字列を返しませんのhexademicalます。
を返す文字列の一部をランダム記号、または通常ている可能性がありhexademical記号-私にとって生成されたstringを中心とする中国hieroglyphsることを確実なparseableによる バイトになります。構文解析 方法。
また、例属 Microsoft商業サーバー -わたしにはわかりませんせです。
"ソリューション:"
までお問い合わせくださいすべてこの例は希望をこの文字列tohex-tobinaryの変換がでの成功を実行しGenerateSaltValueきのようなもの:
public static string ByteArrayToString(byte[] byteArray)
{
StringBuilder hex = new StringBuilder(byteArray.Length * 2);
foreach (byte b in byteArray)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
// Renamed GenerateSaltValue method
private static string GenerateHexSaltString()
{
Random random = new Random();
// Create an array of random values.
byte[] saltValue = new byte[SaltValueSize];
random.NextBytes(saltValue);
string saltValueString = ByteArrayToString(saltValue);
// Return the salt value as a string.
return saltValueString;
}
は、貴社のプログラム"作品"へ どうでしょ変換バイト配列には十六進文字列ょうか。
もの:
- をランダム塩創造ん。
- string-tohex-tobinaryへの変換もworser.
- その他の問題...
い:
読み取って何が本当に差があり、C#とパスワードハッシュおよび暗号化のように:
とするも模索しながら、コード例-なたの別バージョン、プラットフォームです。テレビでも展開しております。