码迷,mamicode.com
首页 > 编程语言 > 详细

关于MD5值加密算法

时间:2015-08-19 16:44:19      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

 public static string getMD5(string str)//该方法获取字符串的md5加密 通常用来验证数据
    {
        System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] pwd = md5.ComputeHash(Encoding.ASCII.GetBytes(str));
        string ret = "";
        for (int i = 0; i < pwd.Length; i++)
        {
            ret += pwd[i].ToString("X");
        }
        return ret;
    }
    private static string GetMD5HashFromFile(string fileName)//该方法用来进行获取文件的md5加密 用来比对 客户端文件是否被篡改
    {
        try
        {
            FileStream file = new FileStream(fileName, FileMode.Open);
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(file);
            file.Close();


            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < retVal.Length; i++)
            {
               // sb.Append(retVal[i].ToString("x2"));
                sb.Append(retVal[i].ToString("X"));
            }
            return sb.ToString();
        }
        catch (Exception ex)
        {
            throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
        }
    }  

版权声明:本文为博主原创文章,未经博主允许不得转载。

关于MD5值加密算法

标签:

原文地址:http://blog.csdn.net/gy373499700/article/details/47782509

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!