MD5加密在软件开发中经常使用, 由于是单向加密也称作不可逆加密, 经常用来做为校验使用, 可以用来校验用户密码, 文件校验
/// <summary> /// MD5字符串加密 /// </summary> /// <param name="input">需要加密的字符串</param> /// <returns>返回32位大写字符串</returns> public static string MD5(string input) { try { using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) { string result = ""; byte[] btInput = System.Text.Encoding.UTF8.GetBytes(input); byte[] btOut = md5.ComputeHash(btInput); for (int i = 0; i < btOut.Length; i++) { result += btOut[i].ToString("X2"); } return result; } } catch { throw; } } /// <summary> /// MD5文件流加密 /// </summary> /// <param name="input">文件流</param> /// <returns>返回32位大写字符串</returns> public static string MD5(Stream input) { try { using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) { string result = ""; byte[] btOut = md5.ComputeHash(input); for (int i = 0; i < btOut.Length; i++) { result += btOut[i].ToString("X2"); } return result; } } catch { throw; } }