Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 来自字符串的MD5哈希_.net_Md5 - Fatal编程技术网

.net 来自字符串的MD5哈希

.net 来自字符串的MD5哈希,.net,md5,.net,Md5,需要从字符串中获取MD5哈希。 获取错误MD5为空。 我想从字符串中获取一个32字符的MD5哈希 using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create("TextToHash")) { byte[] retVal = md5.Hash; StringBuilder sb = new StringBuilder(); for (int i =

需要从字符串中获取MD5哈希。
获取错误MD5为空。
我想从字符串中获取一个32字符的MD5哈希

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"));
    }
}
使用(System.Security.Cryptography.MD5 MD5=
System.Security.Cryptography.MD5.Create(“TextToHash”))
{
字节[]retVal=md5.Hash;
StringBuilder sb=新的StringBuilder();
for(int i=0;i
需要从字符串中获取MD5哈希

然后首先需要将字符串转换为某种形式的二进制数据。如何做到这一点取决于您的需求,但对于某些编码可能是
Encoding.GetBytes
。。。不过,您需要确定是哪种编码。例如,此哈希是否需要与在其他地方创建的哈希匹配

获取错误MD5为空

那是因为你用错了。参数是一个算法名称。几乎可以肯定的是,您应该使用

我猜你想要的是:

byte[] hash;
using (MD5 md5 = MD5.Create())
{
    hash = md5.ComputeHash(Encoding.UTF8.GetBytes(text));
}
// Now convert the binary hash into text if you must...
传递给的字符串不是“要哈希的文本”,而是要使用的算法。我怀疑你想要:

using (System.Security.Cryptography.MD5 md5 = 
   System.Security.Cryptography.MD5.Create())
{
    byte[] retVal = md5.ComputeHash(Encoding.Unicode.GetBytes("TextToHash"));
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < retVal.Length; i++)
    {
        sb.Append(retVal[i].ToString("x2"));
    }
}
使用(System.Security.Cryptography.MD5 MD5=
System.Security.Cryptography.MD5.Create())
{
byte[]retVal=md5.ComputeHash(Encoding.Unicode.GetBytes(“TextToHash”);
StringBuilder sb=新的StringBuilder();
for(int i=0;i
得到
null
返回值的原因是
Create
方法的
string
参数指定了算法,而不是正在散列的文本。没有TextToHash算法,因此返回值为
null

如果它来自SQL vachar(max),UTF8是否是最佳选择?re:无参数重载-是否存在默认算法从一个.Net版本更改为下一个版本的风险?如果是这样,那么同一个字符串从一个版本到下一个版本的散列将不匹配?@xanadont:否。MD5是一种标准化算法。结果改变基本上是一个bug。太好了。谢谢@JonSkeetHi Reed:我有一个问题:我如何知道使用哪种编码?在我的例子中,我需要计算作为查询字符串接收的哈希值。(所以当它被发布到http时,它可能是UTF-8),但是,一旦它被存储在.Net字符串中,它现在会是Unicode(UTF-16)吗,因为这是本机编码?这种转换是为我发生的吗?@JMarsch是的-如果您使用Encoding.Unicode,如果它最初是UTF-8,那么它很可能“起作用”。请记住,如果您的“源”MD5是根据字符串的UTF-8表示形式计算的,那么如果您现在改为使用UTF-16,您将得到不同的MD5。即使字符串中只有ASCII字符,这也是正确的。使用Encoding.Unicode而不是UTF 8将导致与客户机上大多数Javascript库产生的哈希不同的哈希值-大多数转换为utf8。这并不意味着答案是错误的,但我写这篇文章只是作为一个警告,如果有人在客户端和服务器上得到不同的MD5哈希值,那么字符串的初步编码可能会被忽略,让人认为有错误。