【问题标题】:Hashing text with SHA-256 at Windows Forms在 Windows 窗体中使用 SHA256 散列文本
【发布时间】:2012-07-13 19:27:46
【问题描述】:
String inputPass = textBox2.Text;
byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(inputPass);
byte[] inputHashedBytes = Sha256.ComputeHash(inputBytes);
String inputHash = Convert.ToBase64String(inputHashedBytes);

我得到了一些奇怪的输出:

Q9nXCEhAn7RkIOVgBbBeOd5LiH7FWFtDFJ22TMLSoH8=

通过输出哈希看起来像这样:

43d9d70828409fb46420e56005b05e38de4b887ec5585b43149db64cc2d2a07f

【问题讨论】:

  • 定义“奇怪的输出”。

标签: c# winforms utf-8 sha


【解决方案1】:
// this is where you get the actual binary hash
byte[] inputHashedBytes = Sha256.ComputeHash(inputBytes);

// but you want it in a string format, similar to a variety of UNIX tools
string result = BitConverter.ToString(inputHashedBytes)
   // this will remove all the dashes in between each two characters
   .Replace("-", string.Empty)
   // and make it lowercase
   .ToLower();

【讨论】:

  • 谢谢!这是答案。 :)
【解决方案2】:

Encoding.UTF8.GetString 将字节解析为 UTF8 代码点。

SHA256 哈希是任意 256 位数字,不对应任何 Unicode 文本。

您可能希望通过调用BitConverter.ToString() 以十六进制显示二进制值。
您也可以拨打Convert.ToBase64String()

【讨论】:

  • 它更近了,谢谢。我已经编辑了问题以显示新的输出 - 仍然错误......
  • BitConverted 工作!我需要对弦乐进行一些思考......它类似于 43-D9 而不是 43d9
猜你喜欢
  • 2012-09-16
  • 2019-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-23
  • 1970-01-01
  • 2020-05-03
相关资源
最近更新 更多