在C#中的散列字符串 [英] Hash string in c#
本文介绍了在C#中的散列字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 C#
试图获取散列字符串时,我有问题。
我已经尝试在一些网站上的研究,但大多使用文件,以获得哈希值。即使是一些在那里,对于字符串,也有点复杂。而另外一个,我发现它的窗口身份验证这样的网站:
FormsAuthentication.HashPasswordForStoringInConfigFile(tbxPassword.Text.Trim(),MD5)
其实使用散列串在重定向页面更安全的获得文件名的愿望散列后,从查询字符串下载我的目的。
如何获得哈希字符串。
例如:
字符串文件=用户名;
字符串哈希= ??????(用户名);
另外一个问题,使用另一种散列MD5算法可能。
解决方案
使用System.Security.Cryptography;公共静态的byte [] GetHash(字符串inputString)
{
算法的HashAlgorithm = MD5.Create(); //或者使用SHA1.Create();
返回algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
}公共静态字符串GetHashString(字符串inputString)
{
StringBuilder的SB =新的StringBuilder();
的foreach(BYTE B在GetHash(inputString))
sb.Append(b.ToString(X2)); 返回sb.ToString();
}
I have problem when trying get hash string in c#
.
I already try research in few website, but most them using file to get hash. Even some there , for string, there bit complex. And other one I found it for window authentication for web like this:
FormsAuthentication.HashPasswordForStoringInConfigFile(tbxPassword.Text.Trim(), "md5")
Actually my purpose using hash string to more secure in redirect page to get filename wish to download from query string after hashing.
How to get hash in string.
Example:
string file = "username";
string hash = ??????(username);
Another question, it possible using another hashing "md5" algorithm.
解决方案
using System.Security.Cryptography;
public static byte[] GetHash(string inputString)
{
HashAlgorithm algorithm = MD5.Create(); //or use SHA1.Create();
return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
}
public static string GetHashString(string inputString)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in GetHash(inputString))
sb.Append(b.ToString("X2"));
return sb.ToString();
}
这篇关于在C#中的散列字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文