MD5哈希从字符串 [英] MD5 Hash From String

查看:155
本文介绍了MD5哈希从字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要得到的MD5哈希的字符串。
得到一个错误MD5为空。
我绑来从一个字符串32个字符的MD5哈希值。

 使用(System.Security.Cryptography.MD5 MD5 =
       System.Security.Cryptography.MD5.Create(TextToHash))
{
    byte []的retVal的= md5.Hash;
    StringBuilder的SB =新的StringBuilder();
    的for(int i = 0; I< retVal.Length;我++)
    {
        sb.Append(retVal的[I]的ToString(X2));
    }
}
 

解决方案
  

需要得到的MD5哈希的字符串。

那么首先您需要将字符串转换为二进制数据以某种形式。你怎么会取决于您的需求,但它很可能会 Encoding.GetBytes 对于一些编码......你需要解决的编码虽然。这是否哈希需要匹配创建其他地方的哈希,例如?

  

得到一个错误MD5为空。

那是因为你使用的是 MD5.Create 不正确。该参数是一个的算法名称的。你几乎应该只使用参过载代替。

我怀疑你想要的东西,如:

 字节[]哈希值;
使用MD5(MD5 = MD5.Create())
{
    哈希= md5.ComputeHash(Encoding.UTF8.GetBytes(文本));
}
//现在转换成二进制散列成文本,如果你一定要...
 

Need to get MD5 hash from string.
Get an error MD5 is null.
I am tying to get a 32 character MD5 hash from a string.

using (System.Security.Cryptography.MD5 md5 = 
       System.Security.Cryptography.MD5.Create("TextToHash"))
{
    byte[] retVal = md5.Hash;
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < retVal.Length; i++)
    {
        sb.Append(retVal[i].ToString("x2"));
    }
}

解决方案

Need to get MD5 hash from string.

Then first you need to convert your string to binary data in some form. How you do that will depend on your requirements, but it'll probably be Encoding.GetBytes for some encoding... you need to work out which encoding though. Does this hash need to match the hash created somewhere else, for example?

Get an error MD5 is null.

That's because you're using MD5.Create incorrectly. The argument is an algorithm name. You should almost certainly just use the parameterless overload instead.

I suspect you want something like:

byte[] hash;
using (MD5 md5 = MD5.Create())
{
    hash = md5.ComputeHash(Encoding.UTF8.GetBytes(text));
}
// Now convert the binary hash into text if you must...

这篇关于MD5哈希从字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆