gpt4 book ai didi

mysql - 在 Windows 8 中使用 .NET 模拟 MySql 的 PASSWORD() 加密

转载 作者:行者123 更新时间:2023-11-29 00:28:23 24 4
gpt4 key购买 nike

根据 MySQL 文档,PASSWORD() 是双重 SHA1 算法。

在 Win32 中我使用的是这种方法:

public string GenerateMySQLHash(string key)
{
byte[] keyArray = Encoding.UTF8.GetBytes(key);
SHA1Managed enc = new SHA1Managed();
byte[] encodedKey = enc.ComputeHash(enc.ComputeHash(keyArray));
StringBuilder myBuilder = new StringBuilder(encodedKey.Length);

foreach (byte b in encodedKey)
myBuilder.Append(b.ToString("X2"));

return "*" + myBuilder.ToString();
}

SHA1Managed 对象在 Metro .net 框架中不可用,因为安全性内容现在位于 Windows.Security.Cryptography 而不是 System.Security.Cryptography 中。

在文档中,我看到了这个从字符串中获取 SHA1 的例子:

 public String HashMsg(String strMsg)
{
// Convert the message string to binary data.
IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(strMsg, BinaryStringEncoding.Utf8);

// Create a HashAlgorithmProvider object.
HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);

// Hash the message.
IBuffer buffHash = objAlgProv.HashData(buffUtf8Msg);

// Verify that the hash length equals the length specified for the algorithm.
if (buffHash.Length != objAlgProv.HashLength)
{
throw new Exception("There was an error creating the hash");
}

// Convert the hash to a string (for display).
return CryptographicBuffer.EncodeToBase64String(buffHash);
}

但我需要双重 SHA1 算法。有什么方法可以像在 win32 中那样简单地做到这一点?

最佳答案

我终于找到了解决办法:),希望对你有帮助:

        /// <summary>
/// Reverse a string
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}

/// <summary>
/// MySQL PASSWORD encryption
/// </summary>
/// <param name="strMsg"></param>
/// <returns></returns>
public String HashMsg(String strMsg)
{
// Convert the message string to binary data.
IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(strMsg, BinaryStringEncoding.Utf8);

// Create a HashAlgorithmProvider object.
HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);

// Hash the message.
IBuffer buffHash = objAlgProv.HashData(objAlgProv.HashData(buffUtf8Msg));

// Verify that the hash length equals the length specified for the algorithm.
if (buffHash.Length != objAlgProv.HashLength)
{
throw new Exception("There was an error creating the hash");
}

byte[] arrByteNew;
CryptographicBuffer.CopyToByteArray(buffHash, out arrByteNew);
StringBuilder myBuilder = new StringBuilder(arrByteNew.Length);

foreach (var b in arrByteNew)
myBuilder.Append(b.ToString("X2"));

// Concat with the STRING REVERSED
String stringReversed = "*" + myBuilder.ToString() + ReverseString(strMsg);

buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(s3, BinaryStringEncoding.Utf8);
buffHash = objAlgProv.HashData(objAlgProv.HashData(buffUtf8Msg));

if (buffHash.Length != objAlgProv.HashLength)
{
throw new Exception("There was an error creating the hash");
}

CryptographicBuffer.CopyToByteArray(buffHash, out arrByteNew);
myBuilder = new StringBuilder(arrByteNew.Length);

foreach (var b in arrByteNew)
{
myBuilder.Append(b.ToString("X2"));
}

stringReversed = "*" + myBuilder.ToString();

return stringReversed;
}

关于mysql - 在 Windows 8 中使用 .NET 模拟 MySql 的 PASSWORD() 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17832306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com