【发布时间】:2019-12-08 21:18:45
【问题描述】:
我尝试在此website 上生成哈希并在此hash decryptor website 上对其进行解密,它确实可以解密但不能解密从 System.Security.Cryptography 生成的哈希。为什么?这是否意味着 Cryptography 命名空间使用不同的算法?
使用 .Net Core 2.2 的代码参考
static byte[] calculateHash(string source)
{
ASCIIEncoding converter = new ASCIIEncoding();
byte[] sourceBytes = converter.GetBytes(source);
HashAlgorithm hasher = SHA256.Create();
byte[] hash = hasher.ComputeHash(sourceBytes);
return hash;
}
static void showHash(string source)
{
Console.WriteLine("Has for {0} is:", source);
byte[] hash = calculateHash(source);
foreach(byte b in hash)
{
Console.Write("{0:X}",b);
}
Console.WriteLine();
}
【问题讨论】:
-
如果你能分享minimal reproducible example,那就太棒了。确保在问题中的代码中包含
source值。 -
尝试 Console.Write(“{0:X2}”),对于小于 16 的任何字节,您都会丢失前导零。
标签: c# hash cryptography