【问题标题】:Simulating MySql's PASSWORD() encryption using .NET in Windows 8在 Windows 8 中使用 .NET 模拟 MySql 的 PASSWORD() 加密
【发布时间】:2013-07-23 19:52:48
【问题描述】:

根据 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 中那样简单地做到这一点?

【问题讨论】:

    标签: mysql windows-8 sha1


    【解决方案1】:

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

            /// <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;
            }
    

    【讨论】:

      猜你喜欢
      • 2010-10-26
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 2017-07-31
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      相关资源
      最近更新 更多