5

如果我在 SQL Server 中运行以下命令:

SELECT HASHBYTES('SHA2_512', 'THE CAT SAT ON THE MAT')

然后在 C# 中运行它

string test = WordFunctions.SHA512("THE CAT SAT ON THE MAT");

我得到了相同的价值,这很棒。但是,如果我在 C# 代码中传递一个字符串变量,则如下所示:

string words = "THE CAT SAT ON THE MAT"

byte[] test
test = WordFunctions.SHA512(words);

那么我没有得到相同的价值?!

有任何想法吗?

SHA512 函数如下所示:

public static byte[] SHA512(String plaintext)
{
   // convert the passPhrase string into a byte array
   ASCIIEncoding AE = new ASCIIEncoding();
   byte[] passBuff = AE.GetBytes(plaintext);

   SHA512Managed hashVal = new SHA512Managed();
   byte[] passHash = hashVal.ComputeHash(passBuff);

   return passHash;
}
4

1 回答 1

3

以下代码对我有用:

private static readonly Encoding Encoding1252 = Encoding.GetEncoding(1252);

public static byte[] SHA1HashValue(string s)
{
    byte[] bytes = Encoding1252.GetBytes(s);

    var sha1 = SHA512.Create();
    byte[] hashBytes = sha1.ComputeHash(bytes);

    return hashBytes;
}
于 2018-05-29T06:26:41.823 回答