using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Security.Cryptography; namespace MD5加密 { class Program { static void Main(string[] args) { string s = GetMD5("123"); Console.WriteLine(s); Console.ReadLine(); } public static string GetMD5(string str) { MD5 md5 = MD5.Create(); //开始加密 //需要将字符转换成字节数组 byte[] buffer = Encoding.Default.GetBytes(str); byte[] MD5Buffer = md5.ComputeHash(buffer); //字符数组--> 字符串 //将字节数组中每个元素按照指定的编码格式解析成字符串 //直接将数组ToString() //将字节数组中的每个元素ToString() //返回加密好的字符数组 string strNew = ""; for (int i = 0; i < MD5Buffer.Length; i++ ) { //x 转成十六进制 //2 0x0A而不是0xA strNew += MD5Buffer[i].ToString("x2"); //202cb962ac59075b964b07152d234b70 //strNew += MD5Buffer[i].ToString("x"); //202cb962ac5975b964b7152d234b70 } return strNew; } } }