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(); } }
标签:加密,C#,StringBuilder,newBuffer,ToString,sb,MD5 来源: https://www.cnblogs.com/vintom/p/11687243.html