【.NET】MD5的用法(对文件、字符串)

8/10/2015来源:ASP.NET技巧人气:695

【.NET】md5的用法(对文件、字符串)
using System;using System.IO;using System.Security.Cryptography;using System.Text;namespace ConsoleApp_PReventFileTampering{    class Program    {        static void Main(string[] args)        {            //TestGetFileHash();            TestGetMd5();        }        #region 通过Hash值来验证文件是否被篡改        public static void TestGetFileHash()        {            string fileHash = "";            while (true)            {                fileHash = GetFileHash(@"C:\error.txt");                Console.WriteLine("文件MD5-HASH值为:{0}", fileHash);                Console.ReadKey();                //手动修改文件试试,HASH值会变化                //手动将刚才的修改恢复,HASH值会还原            }        }        /// <summary>        /// 通过Hash值来验证文件是否被篡改        /// </summary>        /// <param name="argFilePath">文件路径</param>        /// <returns>输出Hash值</returns>        public static string GetFileHash(string argFilePath)        {            using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())            using (FileStream fs = new FileStream(argFilePath, FileMode.Open, Fileaccess.Read, FileShare.Read))            {                return BitConverter.ToString(md5.ComputeHash(fs)).Replace("-", "");            }        }        #endregion        #region 对字符串用MD5加密        public static void TestGetMd5()        {            while (true)            {                Console.WriteLine("输入要加密的字符串:");                string input = Console.ReadLine();                string md5 = GetMd5Hash2(input);                Console.WriteLine("输出的加密字符串为:" + md5 + "\r\n");                Console.ReadLine();            }        }        /// <summary>        /// 将字符串转换成MD5值存储        /// 【直接使用本方法加密MD5是不安全的,可以通过穷举法获得加密前的字符串】        /// </summary>        /// <param name="argInput">输入的字符串</param>        /// <returns>输出的MD5值</returns>        public static string GetMd5Hash(string argInput)        {            using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())            {                return BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(argInput))).Replace("-", "");            }        }        /// <summary>        /// 多次使用MD5值以及内部拼hashKey法,增加穷举破解的难度        /// </summary>        /// <param name="argInput">输入的字符串</param>        /// <returns>输出特殊处理过的MD5值</returns>        public static string GetMd5Hash2(string argInput)        {            string hashKey = "Kae@#$@!,.Js+{f>.294oE";            using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())            {                string hashCode = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(argInput)))                    .Replace("-", "")                      + BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(hashKey)))                    .Replace("-", "");                return BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(hashCode))).Replace("-", "");            }        }        #endregion    }}