C#MD5加密的两种方式

在开发过程当中,我们经常会用到MD5加密,下面介绍MD5加密的两种方式:

		/// <summary>
		/// MD5字符串加密
		/// </summary>
		/// <param name="txt"></param>
		/// <returns>加密后字符串</returns>
		public static string GenerateMD5(string txt)
		{
			using (MD5 mi = MD5.Create())
			{
				byte[] buffer = Encoding.Default.GetBytes(txt);
				//开始加密
				byte[] newBuffer = mi.ComputeHash(buffer);
				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < newBuffer.Length; i++)
				{
					sb.Append(newBuffer[i].ToString("x2"));
				}
				return sb.ToString();
			}
		}

		/// <summary>
		/// MD5流加密
		/// </summary>
		/// <param name="inputStream"></param>
		/// <returns></returns>
		public static string GenerateMD5(Stream inputStream)
		{
			using (MD5 mi = MD5.Create())
			{
				//开始加密
				byte[] newBuffer = mi.ComputeHash(inputStream);
				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < newBuffer.Length; i++)
				{
					sb.Append(newBuffer[i].ToString("x2"));
				}
				return sb.ToString();
			}
		}

  

posted @ 2019-10-16 17:58  下辈子还当程序员  阅读(43141)  评论(2编辑  收藏  举报