加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在Silverlight中的MD5哈希

发布时间:2020-12-15 03:43:36 所属栏目:百科 来源:网络整理
导读:我正在使用 Windows Phone 7应用程序.我正在使用 this implementation的MD5哈希在silverlight. 我正在使用这个代码 – protected string GetMD5Hash(string input) { byte[] bs = System.Text.Encoding.UTF8.GetBytes(input); MD5Managed md5 = new MD5Manag
我正在使用 Windows Phone 7应用程序.我正在使用 this implementation的MD5哈希在silverlight.

我正在使用这个代码 –

protected string GetMD5Hash(string input)
    {
        byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
        MD5Managed md5 = new MD5Managed();
        byte[] hash = md5.ComputeHash(bs);

        StringBuilder sb = new StringBuilder();
        foreach (byte b in bs)
        {
            sb.Append(b.ToString("x2").ToLower());
        }

        return sb.ToString();    
    }

但是,我没有得到正确的MD5哈希为我提供的输入.我不知道这段代码有什么问题.如果有人在silverlight中使用了this implementation的MD5哈希,你知道我在哪里出错了吗?

解决方法

您正在返回十六进制版本的输入,而不是哈希:
foreach (byte b in bs)

应该

foreach (byte b in hash)

(另一种方法是使用Convert.ToBase64String(哈希),如果你不介意它在Base64而不是十六进制.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读