![23109131_201004071146451.jpg](http://space.itpub.net/attachments/2010/04/23109131_201004071146451.jpg)
using System.Text;
{
byte[] result = Encoding.Default.GetBytes(this.textBox1.Text.Trim());
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] utput = md5.ComputeHash(result);
this.textBox2.Text = BitConverter.ToString(utput).Replace("-", "");//输出加密文本,用空字符替换-
}
使用:例如要保存用户密码,可以把加密后的密码存到数据库中,使用时,只需要将现有的与数据库的相比较就可以,无须解密。
MD5对文件的加密,思路也一样,using System.Security.Cryptography;
public static string FileMd5Value(string path)
{
try
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
MD5CryptoServiceProvider getMd5 = new MD5CryptoServiceProvider();
byte[] hash_byte = getMd5.ComputeHash(fs);//计算指定字节的希哈值
string result = BitConverter.ToString(hash_byte);
result = result.Replace("-", "");
return result;
}
catch (Exception)
{
return null;
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23109131/viewspace-631475/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/23109131/viewspace-631475/