1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ExamManagementsystemOffline.Unitity 8 { 9 using System.Security.Cryptography; 10 11 public class SecurityHelper 12 { 13 /// <summary> 14 /// MD5加密 15 /// </summary> 16 /// <param name="message"></param> 17 /// <returns></returns> 18 public static string MD5Compute(string Message) 19 { 20 //创建MD5对象 21 MD5 mD5 = MD5.Create(); 22 23 //将编码后的字节数组进行哈希计算,然后保存到加密数组中 24 byte[] mD5Bytes = mD5.ComputeHash(Encoding.UTF8.GetBytes(Message)); 25 26 //实例化StringBuilder 对象 27 StringBuilder stringBuilderMD5 = new StringBuilder(); 28 29 //遍历加密数组,将数组元素以16位序列保存到stringBuilderMD5中 30 for (int i = 0; i < mD5Bytes.Length; i++) 31 { 32 stringBuilderMD5.Append(mD5Bytes[i].ToString("X2")); 33 } 34 35 //将stringBuilderMD5对象转成字符串,并返回该值 36 return stringBuilderMD5.ToString(); 37 } 38 } 39 }
原文:https://www.cnblogs.com/ABC-wangyuhan/p/13757307.html