【问题标题】:MD5 hashing in windowsphone 8windowsphone 8中的MD5散列
【发布时间】:2014-02-23 02:09:11
【问题描述】:

嘿,我正在尝试在 windows phone 中将字符串散列到 MD5 ......但是当我调用 MD5 类时,我收到以下错误

找不到类型或命名空间名称“MD5”(您是否缺少 使用指令还是程序集引用?)

PS:我使用了 System.Security.Cryptography 命名空间
那么如何在 windows phone 中使用 MD5 哈希呢? 这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace FluoraPin
{
    class HASHING
    {
        public static string GetMd5Hash(MD5 md5Hash, string input)
        {

            // Convert the input string to a byte array and compute the hash. 
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes 
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data  
            // and format each one as a hexadecimal string. 
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string. 
            return sBuilder.ToString();
        }

        // t verify md5 hashing
        private bool VerifyMd5Hash(MD5 md5Hash, string input, string hash)
        {
            // Hash the input. 
            string hashOfInput = GetMd5Hash(md5Hash, input);

            // Create a StringComparer an compare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

【问题讨论】:

  • 不是答案,而是:我假设您知道 MD5 已损坏?
  • nooop 我不知道!谢谢你有什么建议?
  • 使用SHA-256,通过SHA526Managed class 可用于Windows Phone 7 和8。 answer to another SO question 提供了如何在 C# 中使用 SHA256Managed 进行散列的示例。

标签: c# hash windows-phone md5


【解决方案1】:

我认为错误中的答案是对的:

找不到类型或命名空间名称“MD5”(您是否缺少 using 指令或程序集引用?)

MD5 不是 Windows Phone 的 System.Security.Cryptography 命名空间中的类。请参阅MSDN's System.Security.Cryptography page for Windows Phone 进行确认。

将此与 MSDN's general System.Security.Cryptography page, 进行对比,后者将 MD5 列为命名空间中的一个类。

话虽如此,您确实应该使用SHA-256 或更高版本,而不是 MD5 或 SHA-1 哈希。

SHA-256 散列可通过SHA256Managed 类在您已经使用的Security.Security.Cryptography 命名空间中用于Windows Phone 7 和8。有关如何使用SHA256Managed 的示例,请参阅answer to a related SO question

【讨论】:

  • 但是我需要 MD5 来签署 API 查询
  • 这是一个非常明显的答案。尝试解决问题怎么样?
【解决方案2】:

此人在 C# 中实现了可用于 WP8 的 MD5 散列:

http://upadhyayjitesh.blogspot.com/2013/01/windows-phone-md5-hash-conversion.html

【讨论】:

    【解决方案3】:

    您可以将 Bouncy Castle 作为 NuGet 包添加到您的项目中。它支持 MD5 散列(以及更多加密算法)。有关更多详细信息,请参阅其NuGet 页面。或者它的项目页面“The Legion of the Bouncy Castle

    【讨论】:

      【解决方案4】:

      我尚未测试您的解决方案,但我找到了适合我的解决方案。

      using System.Security.Cryptography;

      class MD5Hash
      {
          public String getHash(String input)
          {
              MD5 md5 = System.Security.Cryptography.MD5.Create();
              byte[] inputBytes = Encoding.ASCII.GetBytes(input);
              byte[] hash = md5.ComputeHash(inputBytes);
      
              StringBuilder sb = new StringBuilder();
      
              for (int i = 0; i < hash.Length; i++)
                  sb.Append(hash[i].ToString("x2"));
      
              return sb.ToString();
          }
      
          public Boolean VerifyHash(String input, String hash)
          {
              String hashOfInput = getHash(input);
      
              StringComparer comparer = StringComparer.OrdinalIgnoreCase;
      
              if (0 == comparer.Compare(hashOfInput, hash))
                  return true;
              else
                  return false;
          }
      }
      

      这将散列你的字符串,完全没有错误。

      此外,您遇到的错误,请检查您没有编译包含文本“客户端配置文件”的 .Net 版本。

      我是新来的,所以如果我完全错了,那么很抱歉,你能否对你的问题更具体一点。

      【讨论】:

      • System.Security.Cryptography.MD5 在 windows phone 上不可用
      猜你喜欢
      • 2012-06-17
      • 2014-11-18
      • 2013-08-29
      • 1970-01-01
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      • 1970-01-01
      相关资源
      最近更新 更多