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

时间:2013-07-24 11:12:12

标签: 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中那样轻松完成吗?

1 个答案:

答案 0 :(得分: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;
        }