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

标签 mysql windows-8 sha1

根据 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/

相关文章:

c# - HMACSHA1 SSL 问题

mysql - 如何使用MySQL高效同步多个客户端之间的数据?

c# - System.Environment.TickCount 的 WinRT 替换

visual-studio-2012 - 如何从命令行构建并启动 Windows 8 Metro 应用程序

windows-8 - 如何实现 Metro 风格应用的 UI 自动化?

java - 用 Java 编码的 SHA1 从相同的 C# 代码中产生不同的结果

google-maps - 获取 MAP API 时 MD5 和 SHA1 有什么区别?

php - 检查日期/时间是否小于提供的日期?

mysql - mysql中两个相同表的区别

java - 从eclipse导入项目到intellij : error with JDBC mysql