1. 首先创建MD5的哈希算法。
((HashAlgorithm)System.Security.Cryptogrophy.CryptoConfig.CreateFromName("MD5")).ComputeHash(System.Text.UTF8.GetBytes(input))
2.计算哈希值
使用方法:ComputeHash(byte[] value);
3.转化成字符串.
1
protected
virtual
string
HashMD5(
string
input)
2
{
3
byte[] result = ((HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(System.Text.Encoding.UTF8.GetBytes(input));
4
StringBuilder output = new StringBuilder(16);
5
6
for (int i = 0; i < result.Length; i++)
7
{
8
// convert from hexa-decimal to character
9
output.Append((result[i]).ToString( "x2", System.Globalization.CultureInfo.InvariantCulture));
10
}
11
return output.ToString();
12
}
13
protected
virtual
string
HashMD5(
string
input)2
{3
byte[] result = ((HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(System.Text.Encoding.UTF8.GetBytes(input));4
StringBuilder output = new StringBuilder(16); 5
6
for (int i = 0; i < result.Length; i++) 7
{ 8
// convert from hexa-decimal to character9
output.Append((result[i]).ToString( "x2", System.Globalization.CultureInfo.InvariantCulture)); 10
}11
return output.ToString();12
}
13
本文介绍了一种使用MD5哈希算法生成字符串哈希值的方法。通过创建MD5哈希对象并调用ComputeHash方法计算输入字符串的哈希值,最后将得到的字节数组转换为十六进制字符串形式。

650

被折叠的 条评论
为什么被折叠?



