ITPub博客

首页 > Linux操作系统 > Linux操作系统 > C# MD5加密

C# MD5加密

原创 Linux操作系统 作者:fangqm 时间:2010-04-07 11:47:50 0 删除 编辑
MD5的全称是message-digest algorithm 5
MD5具有很好的安全性,它具有不可逆的特征,解密的可能性很小。
 
winform来个简单的例子,如图
 
using System.Security.Cryptography;
using System.Text;
 
点击MD5加密按钮,实现对要加密字符串的加密。
//MD5加密按钮事件
 private void button1_Click_1(object sender, EventArgs e)
        {
            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博客 ” ,链接:https://blog.itpub.net/23109131/viewspace-631475/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
创业项目 https://u.rscode.cn

注册时间:2010-03-24

  • 博文量
    297
  • 访问量
    784622