一:字符串加密: public static String GetMD5(string input) { System.Security.Cryptography.MD5CryptoServiceProvider x=newSystem.Security.Cryptography.MD5CryptoServiceProvider(); byte[]bs =System.Text.Encoding.UTF8.GetBytes(input); bs =x.ComputeHash(bs); System.Text.StringBuilder s =newSystem.Text.StringBuilder(); foreach(byte b inbs) { s.Append(b.ToString("x2").ToLower()); } returns.ToString(); } public static string GetMD5(string sDataIn) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] bytValue, bytHash; bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn); bytHash = md5.ComputeHash(bytValue); md5.Clear(); string sTemp = ""; for (int i = 0; i < bytHash.Length; i++) { sTemp += bytHash[i].ToString("X").PadLeft(2, '0'); } return sTemp.ToLower(); } 二:文件加密: /// <summary> /// 计算文件MD5值 /// </summary> /// <param name="str">需要计算的文件路径</param> /// <returns>MD5值</returns> public static string MD5Value(String filepath) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] md5ch; using (FileStream fs = File.OpenRead(filepath)) { md5ch = md5.ComputeHash(fs); } md5.Clear(); string strMd5 = ""; for (int i = 0; i < md5ch.Length - 1; i++) { strMd5 += md5ch[i].ToString("x").PadLeft(2, '0'); } return strMd5; }