首页 > Windows开发 > 详细

C# MD5

时间:2016-08-23 14:59:07      阅读:252      评论:0      收藏:0      [点我收藏+]

没什么好写的

技术分享
    static class MyMD5
    {
        public static string GetMD5Hash(string Message,bool MD5_Mode)
        {
            try
            {
                byte[] result = Encoding.Default.GetBytes(Message);
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                byte[] output = md5.ComputeHash(result);
                if (MD5_Mode == true) return BitConverter.ToString(output).Replace("-", "");//32位MD5值
                else return BitConverter.ToString(output, 4, 8).Replace("-", "");           //16位MD5值
            }
            catch { MessageBox.Show("校验失败");return ""; }

        }
        public static string GetMD5HashFromFile(string fileName,bool MD5_Mode)
        {
            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"));
                }
                if(MD5_Mode==true) return sb.ToString().ToUpper();
                else return sb.ToString().Substring(8, 16).ToUpper();

            }
            catch { MessageBox.Show("校验失败"); return ""; }
        }
    }
MyMD5

 

C# MD5

原文:http://www.cnblogs.com/xzhblogs/p/5799079.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!